Compare commits
106
Commits
8f768277e1
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
480343d5a5 | ||
|
|
77a0d1010c | ||
|
|
f56b345598 | ||
|
|
a4bcd40e7b | ||
|
|
4b575d3a1d | ||
|
|
61107848f8 | ||
|
|
1e3406a7ae | ||
|
|
ef6c671903 | ||
|
|
ef2f8c30f0 | ||
|
|
73005a3c05 | ||
|
|
918134df69 | ||
|
|
d64d864175 | ||
|
|
62de3e3a8b | ||
|
|
9f024dc6ac | ||
|
|
05c2706eaf | ||
|
|
ac23d7d1d9 | ||
|
|
3c8abb6fbf | ||
|
|
640e5a5dd0 | ||
|
|
3fa643828d | ||
|
|
888765c47f | ||
|
|
f3407ebfdf | ||
|
|
56676effc3 | ||
|
|
d7d6382b60 | ||
|
|
007818b03f | ||
|
|
b24045812b | ||
|
|
0d943fe3dc | ||
|
|
6e82a02e64 | ||
|
|
08ff14d0ac | ||
|
|
b17da6ab25 | ||
|
|
56ca775405 | ||
|
|
8ee99e8ce8 | ||
|
|
5ea1d50ad1 | ||
|
|
6ef7486ca4 | ||
|
|
7c15cfd26d | ||
|
|
2ca277d858 | ||
|
|
758e25452d | ||
|
|
60d375144d | ||
|
|
1c816019b1 | ||
|
|
ceff6f98d9 | ||
|
|
8395e60f87 | ||
|
|
a92b951540 | ||
|
|
d5938f94ed | ||
|
|
991328ff0e | ||
|
|
1835221096 | ||
|
|
e7a6245ac2 | ||
|
|
da43b6c80c | ||
|
|
eb67424a1c | ||
|
|
3776475db4 | ||
|
|
54b9186e52 | ||
|
|
fe684faf70 | ||
|
|
22f834ef45 | ||
|
|
7b249a7222 | ||
|
|
57529fd1ea | ||
|
|
acbb833eca | ||
|
|
6912669754 | ||
|
|
7388ad3451 | ||
|
|
b13c9f47be | ||
|
|
f14a0e5fe7 | ||
|
|
505ed2bfe2 | ||
|
|
548ae541b2 | ||
|
|
05883b1ee6 | ||
|
|
ad015e1e9c | ||
|
|
62e3e49ad1 | ||
|
|
338591691f | ||
|
|
f56109836e | ||
|
|
b1a45902b4 | ||
|
|
7d8874c8cd | ||
|
|
a06db72c65 | ||
|
|
afc8909d3f | ||
|
|
990ebdb158 | ||
|
|
1a4888df50 | ||
|
|
2978483ea9 | ||
|
|
43742e8d99 | ||
|
|
6793f77733 | ||
|
|
adffac7360 | ||
|
|
fdb7af7ef5 | ||
|
|
48291ab641 | ||
|
|
6b34293cd3 | ||
|
|
5cb42c8dfb | ||
|
|
759ae91745 | ||
|
|
179484642f | ||
|
|
6cc45d7413 | ||
|
|
3933214953 | ||
|
|
8561c3f85e | ||
|
|
d2350a6427 | ||
|
|
0845f80768 | ||
|
|
3265c149da | ||
|
|
57710a7fe3 | ||
|
|
5b2eb91c5c | ||
|
|
11ad6d4376 | ||
|
|
6c3d81920e | ||
|
|
108c6ce03d | ||
|
|
6bffc53779 | ||
|
|
3d828a61ab | ||
|
|
7f07c111ac | ||
|
|
8218039c64 | ||
|
|
f705c67fc7 | ||
|
|
c8b3054527 | ||
|
|
8ec3df541e | ||
|
|
8ee144745a | ||
|
|
64ad9baf39 | ||
|
|
704e33638f | ||
|
|
eddb7edea4 | ||
|
|
1c9ddcd48a | ||
|
|
30ff970c38 | ||
|
|
f99980052b |
No files matched your search
+33
-66
@@ -18,6 +18,21 @@ CACHE_MEMORY_MAX_ENTRIES=2000
|
||||
# every 5 minutes brings an over-budget directory back to 90% of this (default 20000).
|
||||
IMAGING_CACHE_MAX_ENTRIES=20000
|
||||
|
||||
# --- CACHE TTL TUNING (seconds; defaults apply when unset) ---
|
||||
# Higher = less DB load but staler data. Publishing news rotates its cache
|
||||
# revision immediately, so news TTLs only delay content that bypasses that path.
|
||||
CMS_HOME_PAYLOAD_TTL_SEC=15
|
||||
CMS_NEWS_LIST_TTL_SEC=300
|
||||
CMS_NEWS_ARCHIVE_TTL_SEC=60
|
||||
CMS_SEARCH_TTL_SEC=30
|
||||
CMS_ONLINE_COUNT_TTL_SEC=10
|
||||
CMS_COUNTERS_TTL_SEC=300
|
||||
CMS_SITE_SETTINGS_TTL_SEC=300
|
||||
CMS_SITE_SETTINGS_MEMORY_TTL_SEC=60
|
||||
# Boot-time cache warm-up (primes hot keys after each deploy). Set false on
|
||||
# tiny hosts where the boot COUNT(*) burst is undesirable.
|
||||
CMS_WARMUP_ENABLED=true
|
||||
|
||||
# --- CORE RUNTIME & PERFORMANCE FLAGS ---
|
||||
NODE_ENV=production
|
||||
PORT=3002
|
||||
@@ -78,72 +93,17 @@ CLOUDFLARE_AUTO_BLOCK_ENABLED=true
|
||||
# Override for tests/staging (production uses the public endpoint by default).
|
||||
CLOUDFLARE_API_BASE_URL=https://api.cloudflare.com/client/v4
|
||||
|
||||
# --- CROWDSEC API (community reputation auto-block, optional) ---
|
||||
# Free CTI API key: https://app.crowdsec.net/ → Settings → CTI API Keys.
|
||||
# When set, the anti-DDoS gate checks the community reputation of repeat
|
||||
# offenders (CTI GET /smoke/{ip}) and immediately hard-blocks known-bad IPs.
|
||||
# Lookups only happen for IPs that already tripped a rate bucket and are
|
||||
# cached in Redis for 1h, so quota usage stays minimal.
|
||||
CROWDSEC_API_KEY=
|
||||
# Runtime toggle for reputation-based auto-blocking (also overridable live
|
||||
# from the admin panel). Requires CROWDSEC_API_KEY.
|
||||
CROWDSEC_AUTO_BLOCK_ENABLED=true
|
||||
# Minimum malevolence score 0-5 (CrowdSec scale; 4-5 = "malicious") before an
|
||||
# IP is treated as known-bad. IPs with false-positive tags are never blocked.
|
||||
CROWDSEC_BLOCK_SCORE=4
|
||||
# How long a CrowdSec-confirmed bad IP stays blocked (seconds).
|
||||
CROWDSEC_BLOCK_TTL_SECONDS=86400
|
||||
# Endpoint — override only for tests/staging.
|
||||
CROWDSEC_CTI_BASE_URL=https://cti.api.crowdsec.net/v2
|
||||
# Daily enrichment-call ceiling (freemium plan ≈ 10k/day). Once today's
|
||||
# counter reaches it, reputation lookups pause until tomorrow so a spread
|
||||
# DDoS cannot silently burn the whole quota. 0 = unlimited.
|
||||
CROWDSEC_CTI_DAILY_QUOTA=10000
|
||||
# How many new community-reputation blocks within a 5-minute window justify an
|
||||
# ops alert (quota/backoff/report alerts all use HEALTH_ALERT_COOLDOWN_MIN).
|
||||
CROWDSEC_ALERT_BLOCK_BURST=10
|
||||
|
||||
# --- CROWDSEC SIGNAL PUSH (share our blocks back, optional) ---
|
||||
# Opt-in: pushes blocked IPs + behaviors to the CrowdSec Central API (CAPI) so
|
||||
# the community blocklist protects other members too. Set to "true" to enable.
|
||||
# Requires watcher credentials — either set both CROWDSEC_REPORT_MACHINE_ID
|
||||
# (48 chars, [A-Za-z0-9]) and CROWDSEC_REPORT_PASSWORD now, or leave them
|
||||
# unset and let the app generate a stable pair persisted in Redis automatically.
|
||||
CROWDSEC_REPORT_ENABLED=false
|
||||
CROWDSEC_REPORT_MACHINE_ID=
|
||||
CROWDSEC_REPORT_PASSWORD=
|
||||
# Optional: attachment key from https://app.crowdsec.net → Console settings —
|
||||
# links our watcher to your account so pushed signals show up there.
|
||||
CROWDSEC_REPORT_ENROLL_KEY=
|
||||
# Central API base — override only for tests/staging.
|
||||
CROWDSEC_CAPI_BASE_URL=https://api.crowdsec.net/v3
|
||||
|
||||
# --- CROWDSEC LOCAL (opt-in engine on this Docker host, no proxy changes) ---
|
||||
# App-layer LAPI bouncer: the anti-DDoS gate asks the local engine per client
|
||||
# IP (short-cached) and blocks ban/captcha decisions before its own buckets.
|
||||
# Start everything with `bash cms security`; it writes the key below into .env
|
||||
# and starts the CrowdSec engine bound to 127.0.0.1. Set to "true" to load the
|
||||
# bouncer without the local engine (not recommended).
|
||||
CROWDSEC_LOCAL_ENABLED=false
|
||||
# Host access-log directory mounted into the engine for detection (Nginx only).
|
||||
CROWDSEC_NGINX_LOG_DIR=/var/log/nginx
|
||||
# Change LAPI port AND LAPI URL together when 18080 is already taken.
|
||||
CROWDSEC_LAPI_PORT=18080
|
||||
CROWDSEC_LAPI_URL=http://127.0.0.1:18080
|
||||
# Generated by `bash cms security`; keep in .env, never commit a value.
|
||||
CROWDSEC_LAPI_API_KEY=
|
||||
# IP blocklist sync (`bash cms security blocklists`): space-separated URLs, by
|
||||
# default Spamhaus DROP/EDROP, DShield, CINS, Greensnow, StopForumSpam,
|
||||
# blocklist.de, Emerging Threats, abuse.ch Feodo/SSLBL/URLhaus, IPsum,
|
||||
# Firehol ipsets and Tor exit nodes. Requires internet to fetch; detection and
|
||||
# blocking stay local.
|
||||
#CROWDSEC_BLOCKLIST_SOURCES=https://www.spamhaus.org/drop/drop.txt https://example.org/list.txt
|
||||
# Expiration for each blocklist decision (re-synced keeps them fresh).
|
||||
#CROWDSEC_BLOCKLIST_DURATION=24h
|
||||
# Combined cap per sync (safety valve against excessive decisions).
|
||||
#CROWDSEC_BLOCKLIST_MAX_DECISIONS=1000000
|
||||
# Comma-separated IPs/CIDRs that a sync must always skip (allowlist).
|
||||
#CROWDSEC_BLOCKLIST_ALLOW=1.2.3.4,10.0.0.0/8
|
||||
# --- CAPTCHA (env fallback; admin settings win when set) ---
|
||||
# Enable a captcha on register/login/forgot/resend without touching the DB.
|
||||
# "none" (default), "turnstile", "recaptcha" or "hcaptcha". Example:
|
||||
# CAPTCHA_PROVIDER=turnstile
|
||||
CAPTCHA_PROVIDER=
|
||||
# Provider keys. The SITE key is public (it ships to every browser), the
|
||||
# SECRET is server-only and must never be committed — set it on the host or as
|
||||
# a masked CI/CD variable. Cloudflare Turnstile keys:
|
||||
# https://dash.cloudflare.com/turnstile
|
||||
TURNSTILE_SITE_KEY=
|
||||
TURNSTILE_SECRET_KEY=
|
||||
|
||||
# --- PATHS ---
|
||||
BADGE_UPLOAD_DIR=./public/assets/images/badges
|
||||
@@ -156,6 +116,13 @@ DB_BACKUP_KEEP=7
|
||||
# Minutes between repeat health-fail alerts from jobs-worker (default 15).
|
||||
HEALTH_ALERT_COOLDOWN_MIN=15
|
||||
|
||||
# --- FURNITURE ASSETS ---
|
||||
# Extension the local Nitro client requests for furniture bundles. The CMS
|
||||
# writes new/imported/repaired bundles with this extension and prefers it when
|
||||
# resolving existing ones (the other stays readable as a legacy fallback).
|
||||
# ".hab" (default) or ".nitro".
|
||||
FURNI_BUNDLE_EXT=.hab
|
||||
|
||||
# --- RCON (Low Latency Loop) ---
|
||||
RCON_HOST=127.0.0.1
|
||||
RCON_PORT=3003
|
||||
|
||||
+87
-33
@@ -8,24 +8,15 @@ on:
|
||||
branches: [main, master]
|
||||
workflow_dispatch:
|
||||
|
||||
# Reuse the Playwright browsers that ship with the host runner (snapped to
|
||||
# the root HOME cache instead of a fresh per-job HOME) so `playwright install`
|
||||
# is a near-instant no-op instead of a ~100s CDN download on every run.
|
||||
env:
|
||||
PLAYWRIGHT_BROWSERS_PATH: /opt/ms-playwright
|
||||
|
||||
jobs:
|
||||
# ─────────────────────────────────────────────
|
||||
# Fast quality gate: toolchain, install, dependabot audit,
|
||||
# lint, i18n contracts & typecheck. No heavy test suites here.
|
||||
# Draait op de host (self-hosted) waar Node 26 + pnpm 11
|
||||
# geïnstalleerd zijn en internet beschikbaar is.
|
||||
# ─────────────────────────────────────────────
|
||||
check:
|
||||
runs-on: self-hosted
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
repository: ${{ gitea.repository }}
|
||||
token: ${{ gitea.token }}
|
||||
@@ -33,6 +24,12 @@ jobs:
|
||||
- name: Toolchain check
|
||||
run: node scripts/check-node-toolchain.mjs
|
||||
|
||||
- name: README version sync
|
||||
run: node scripts/sync-readme-versions.mjs --check
|
||||
|
||||
- name: Deploy port-selection tests
|
||||
run: bash scripts/ci-deploy-ports.test.sh
|
||||
|
||||
- name: Install dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
@@ -48,17 +45,12 @@ jobs:
|
||||
- name: Typecheck
|
||||
run: pnpm typecheck
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# Test suites. Parallel jobs (host runner capacity >= 3) so unit,
|
||||
# integration and UI tests each get a worker instead of running
|
||||
# back-to-back inside the check job (~2min wall-time saving).
|
||||
# ─────────────────────────────────────────────
|
||||
tests-unit:
|
||||
needs: check
|
||||
runs-on: self-hosted
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
repository: ${{ gitea.repository }}
|
||||
token: ${{ gitea.token }}
|
||||
@@ -83,10 +75,10 @@ jobs:
|
||||
|
||||
tests-integration:
|
||||
needs: check
|
||||
runs-on: self-hosted
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
repository: ${{ gitea.repository }}
|
||||
token: ${{ gitea.token }}
|
||||
@@ -99,10 +91,10 @@ jobs:
|
||||
|
||||
tests-ui:
|
||||
needs: check
|
||||
runs-on: self-hosted
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
repository: ${{ gitea.repository }}
|
||||
token: ${{ gitea.token }}
|
||||
@@ -110,7 +102,6 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
# Compare against reviewed Linux references; updates are explicit.
|
||||
- name: Install UI test browser
|
||||
run: pnpm exec playwright install chromium
|
||||
|
||||
@@ -128,14 +119,82 @@ jobs:
|
||||
test-results/ui/
|
||||
retention-days: 14
|
||||
|
||||
# Validate branch/PR Docker images before integration into a deployment branch.
|
||||
# Cross-browser imaging: the deploy gate only runs chromium, so this verifies
|
||||
# a real render (decode + APNG animation + visual baseline) across every
|
||||
# bundled engine against the public site. Blocking on purpose: this is the
|
||||
# signal that avatar imaging works on every system, so a failure here must
|
||||
# stop the pipeline. IMAGING_EDGE marks the target as edge-fronted, enabling
|
||||
# the public-routing smoke and skipping the CMS-only /imaging assertions.
|
||||
tests-imaging-browsers:
|
||||
needs: check
|
||||
if: gitea.event_name == 'push' && (gitea.ref_name == 'main' || gitea.ref_name == 'master')
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
repository: ${{ gitea.repository }}
|
||||
token: ${{ gitea.token }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Install cross-browser engines
|
||||
run: pnpm exec playwright install --with-deps chromium firefox webkit
|
||||
|
||||
- name: Cross-browser imaging checks
|
||||
env:
|
||||
PLAYWRIGHT_BASE_URL: https://epicnabbo.nl
|
||||
IMAGING_EDGE: "1"
|
||||
run: pnpm test:imaging
|
||||
|
||||
# Advisory live-quality checks: broken uploads/theme assets, legacy malformed
|
||||
# avatar figures and warm-page latency. These depend on live content and data
|
||||
# rather than the code under test, so they are informational only —
|
||||
# `continue-on-error` keeps the pipeline green while still surfacing the
|
||||
# signal in the job log and uploaded report. The deterministic caching contract
|
||||
# is enforced by the blocking deploy gate instead.
|
||||
tests-quality:
|
||||
needs: check
|
||||
if: gitea.event_name == 'push' && (gitea.ref_name == 'main' || gitea.ref_name == 'master')
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
repository: ${{ gitea.repository }}
|
||||
token: ${{ gitea.token }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: pnpm install --frozen-lockfile
|
||||
|
||||
- name: Install quality test browser
|
||||
run: pnpm exec playwright install chromium
|
||||
|
||||
- name: Advisory quality checks
|
||||
continue-on-error: true
|
||||
env:
|
||||
PLAYWRIGHT_BASE_URL: https://epicnabbo.nl
|
||||
run: pnpm test:e2e:quality
|
||||
|
||||
- name: Upload quality results
|
||||
if: always()
|
||||
uses: https://gitea.com/actions/gitea-upload-artifact@62ac910c5d3dfa85c7cb2df15afe2e342b2407c2
|
||||
with:
|
||||
name: quality-results
|
||||
path: |
|
||||
playwright-report/quality/
|
||||
test-results/quality/
|
||||
if-no-files-found: warn
|
||||
retention-days: 14
|
||||
|
||||
preflight:
|
||||
needs: [tests-unit, tests-integration, tests-ui]
|
||||
if: gitea.event_name == 'pull_request' || (gitea.event_name == 'push' && startsWith(gitea.ref, 'refs/heads/codex/'))
|
||||
runs-on: self-hosted
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
repository: ${{ gitea.repository }}
|
||||
token: ${{ gitea.token }}
|
||||
@@ -158,18 +217,13 @@ jobs:
|
||||
if-no-files-found: warn
|
||||
retention-days: 14
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# Docker build & deploy
|
||||
# Draait op de host (self-hosted) zodat Docker
|
||||
# toegang heeft tot de daemon en volumes.
|
||||
# ─────────────────────────────────────────────
|
||||
deploy:
|
||||
needs: [tests-unit, tests-integration, tests-ui]
|
||||
if: gitea.event_name == 'push' && (gitea.ref_name == 'main' || gitea.ref_name == 'master')
|
||||
runs-on: self-hosted
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
repository: ${{ gitea.repository }}
|
||||
token: ${{ gitea.token }}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
name: Gitea Actions Runner Test
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
test-job:
|
||||
runs-on: self-hosted
|
||||
steps:
|
||||
- name: Check Host Environment
|
||||
run: |
|
||||
echo "The v3.5.0 runner works!"
|
||||
echo "Current date/time on VPS:"
|
||||
date
|
||||
echo "Running kernel version:"
|
||||
uname -a
|
||||
|
||||
- name: Test Bash Command
|
||||
run: echo "Greetings from the host runner!"
|
||||
@@ -15,6 +15,8 @@ public/cache/
|
||||
|
||||
prod.log
|
||||
*.log
|
||||
# Imager health-alert state (last-known up/down + timestamp)
|
||||
logs/imager-health.state
|
||||
|
||||
# Database backups
|
||||
db_backup_*.sql
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
image: node:26
|
||||
|
||||
stages:
|
||||
- test
|
||||
- build
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- node_modules/
|
||||
|
||||
before_script:
|
||||
- corepack enable
|
||||
- pnpm install --frozen-lockfile
|
||||
|
||||
lint:
|
||||
stage: test
|
||||
script:
|
||||
- pnpm run lint
|
||||
|
||||
typecheck:
|
||||
stage: test
|
||||
script:
|
||||
- pnpm run typecheck
|
||||
|
||||
test:
|
||||
stage: test
|
||||
script:
|
||||
- pnpm run test
|
||||
|
||||
build:
|
||||
stage: build
|
||||
script:
|
||||
- pnpm run build
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"WARNING": "This file is automatically generated by Gitea Runner. Do not edit it manually unless you know what you are doing. Removing this file will cause Gitea Runner to re-register as a new runner.",
|
||||
"id": 22,
|
||||
"uuid": "ffb52201-e18e-4a0f-96e9-cf8a0b849365",
|
||||
"name": "v5-runner",
|
||||
"token": "0484b5a82d3bda9a62972a6d2646ca7cb90bc4e2",
|
||||
"address": "https://gitlab.epicnabbo.nl/",
|
||||
"labels": [
|
||||
"self-hosted:host",
|
||||
"ubuntu-latest:docker://node:18-bullseye",
|
||||
"debian-latest:docker://debian:bullseye-slim"
|
||||
],
|
||||
"ephemeral": false
|
||||
}
|
||||
Whitespace-only changes.
+35
-27
@@ -1,40 +1,50 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
# Pin the runtime to the supported engine; update both stages deliberately.
|
||||
FROM node:26.10.0-alpine AS migrations
|
||||
WORKDIR /app
|
||||
ENV NEXT_TELEMETRY_DISABLED=1
|
||||
# Keep the bootstrap aligned with package.json packageManager.
|
||||
# The apk cache is persisted in a BuildKit cache mount so git is not
|
||||
# re-downloaded on every build.
|
||||
|
||||
# Installeer git, bash en pnpm v12. bash is nodig voor
|
||||
# scripts/with-memory-cap.sh (gebruikt bashisme zoals arrays en BASH_REMATCH);
|
||||
# Alpine levert geen bash mee.
|
||||
RUN --mount=type=cache,target=/var/cache/apk \
|
||||
apk add --no-cache git \
|
||||
&& npm install -g pnpm@11.25.0
|
||||
# The pnpm store is kept in a BuildKit cache mount that persists across builds
|
||||
# on the builder. This is what stops disk usage from growing unbounded: the
|
||||
# downloaded dependency store is shared and reused instead of being copied into
|
||||
# a fresh image layer on every build. Unlike an image layer it is also prunable
|
||||
# independently, so a hard cap (see ci-deploy.sh) keeps it bounded.
|
||||
ENV PNPM_HOME=/pnpm PNPM_STORE=/pnpm/store
|
||||
# pnpm-workspace.yaml + .npmrc must be present too: the lockfile records the
|
||||
# overrides from pnpm-workspace.yaml, and --frozen-lockfile rejects a build
|
||||
# where the workspace config is absent (ERR_PNPM_LOCKFILE_CONFIG_MISMATCH).
|
||||
apk add --no-cache git bash \
|
||||
&& npm install -g pnpm@12.10.1
|
||||
|
||||
# Stel het PATH zo in dat Alpine pnpm gegarandeerd overal herkent
|
||||
ENV PNPM_HOME="/usr/local/share/pnpm"
|
||||
ENV PATH="$PNPM_HOME:/usr/local/bin:$PATH"
|
||||
|
||||
COPY package.json pnpm-lock.yaml* pnpm-workspace.yaml* .npmrc* ./
|
||||
# pnpm fetch: download all deps into the shared cache-mounted store.
|
||||
RUN --mount=type=cache,target=/pnpm \
|
||||
pnpm fetch --ignore-scripts
|
||||
# Install offline from the cache-mounted store; the store itself stays in the
|
||||
# build cache between builds.
|
||||
RUN --mount=type=cache,target=/pnpm \
|
||||
pnpm install --frozen-lockfile --ignore-scripts --offline
|
||||
|
||||
# Voer de installatie uit met de pnpm v12 store cache-mount
|
||||
RUN --mount=type=cache,target=/root/.local/share/pnpm/store \
|
||||
pnpm install --frozen-lockfile --ignore-scripts
|
||||
|
||||
COPY . .
|
||||
ARG NEXT_DEPLOYMENT_ID="unknown"
|
||||
LABEL org.opencontainers.image.revision="$NEXT_DEPLOYMENT_ID"
|
||||
|
||||
FROM migrations AS builder
|
||||
ARG NEXT_DEPLOYMENT_ID="unknown"
|
||||
ENV NEXT_DEPLOYMENT_ID="$NEXT_DEPLOYMENT_ID"
|
||||
# Fixture values exist only for this build command; production secrets are runtime-only.
|
||||
# Cache Next.js build output and webpack caches so rebuilds only redo the
|
||||
# changed parts.
|
||||
|
||||
# The build runs the webpack builder (see the `build` script in package.json).
|
||||
# Turbopack's compiler is a single native process that grows past 12GB RSS on
|
||||
# this 329-route app and gets OOM-killed; webpack peaks around 5GB. A
|
||||
# --max-old-space-size cap does NOT help, because that memory is native
|
||||
# Turbopack memory rather than the V8 heap.
|
||||
#
|
||||
# `pnpm run build` goes through scripts/with-memory-cap.sh. BuildKit's build
|
||||
# container has /sys/fs/cgroup mounted read-only (no cgroup MemoryMax) and
|
||||
# `ulimit -v` breaks V8-based builds (see the script header), so this stage
|
||||
# explicitly opts out of the cap. The real bound here is the webpack builder
|
||||
# + the V8 heap cap above, and the build runs isolated in its own container,
|
||||
# not on the host; the host itself is protected by the same wrapper through
|
||||
# systemd.
|
||||
ENV NODE_OPTIONS="--max-old-space-size=4096"
|
||||
ENV CMS_MEMORY_CAP_BACKEND=none
|
||||
|
||||
# Bouw de Next.js applicatie met caching
|
||||
RUN --mount=type=cache,target=/app/.next/cache \
|
||||
DATABASE_URL="mysql://build:[email protected]:9/build" \
|
||||
HOTEL_NAME="Build fixture" APP_URL="http://localhost:3002" \
|
||||
@@ -62,8 +72,6 @@ COPY --from=builder --chown=nextjs:nextjs /app/drizzle/migrations ./drizzle/migr
|
||||
COPY --chown=nextjs:nextjs scripts/docker-start.mjs ./docker-start.mjs
|
||||
USER nextjs
|
||||
EXPOSE 3002
|
||||
# Self-contained healthcheck so `docker run` (ci-deploy) also gets Docker-level
|
||||
# health; docker-compose overrides this with its own probe if needed.
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
|
||||
CMD ["node", "-e", "fetch('http://127.0.0.1:'+(process.env.PORT||'3002')+'/api/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
|
||||
ENTRYPOINT ["/sbin/tini", "--"]
|
||||
|
||||
@@ -10,14 +10,21 @@ Features a premium animated homepage (typewriter hero, floating orbs, scroll cou
|
||||
|
||||
| Component | Version | Notes |
|
||||
| --------------- | -------------- | ---------------------------------------- |
|
||||
| Node.js | 26.9.0 | Current release pinned in `.nvmrc` |
|
||||
| pnpm | >= 11.25.0 | Recommended package manager |
|
||||
| Node.js | 26.10.0 | Pinned in `.nvmrc` (`>=26.10.0 <27`) |
|
||||
| pnpm | 12.11.2 | Pinned via `packageManager` |
|
||||
| npm | >= 11.x | Supported alternative |
|
||||
| yarn | >= 4.x | Supported alternative |
|
||||
| MySQL / MariaDB | 8.0+ / 10.6+ | Shared with the emulator |
|
||||
| Docker | 24+ | Optional — for containerized deployment |
|
||||
| Valkey | 8.x+ | Optional — caching, rate limiting, SSE |
|
||||
|
||||
Core stack: **Next.js 16.4.1** (App Router) · **React 19.3.1** · **TypeScript 7.0.2** · **Drizzle ORM 0.45.4** · **Zod 4.6.7** · **Vitest 5.0.4** · **Biome 2.5.15** · **Playwright 1.64.1**.
|
||||
|
||||
> Builds must run through the `pnpm` scripts. This host has no swap and
|
||||
> `vm.overcommit_memory=0`, so an uncapped `next build` gets OOM-killed by the
|
||||
> kernel and can take the database and the live release down with it. See
|
||||
> [Memory-capped commands](#memory-capped-commands).
|
||||
|
||||
---
|
||||
|
||||
## Quick Start
|
||||
@@ -102,6 +109,10 @@ pnpm build && pnpm start # or: npm run build && npm start
|
||||
|
||||
Open `http://localhost:3002` in your browser.
|
||||
|
||||
> Always go through these scripts. `next build` is refused outright when it is
|
||||
> not running under the memory cap — see
|
||||
> [Memory-capped commands](#memory-capped-commands).
|
||||
|
||||
### 6. First Login
|
||||
|
||||
1. Register an account at `/register`, or log in with an existing emulator account.
|
||||
@@ -180,7 +191,9 @@ it does not fetch changes to this Git-built CMS. Use the updater for releases.
|
||||
A clone does **not** install an automatic scheduler. If desired, explicitly add a
|
||||
cron entry with the absolute path of **your** checkout; keep logs in its `logs`
|
||||
directory. Do not configure both CI and Compose updates for the same instance.
|
||||
The updater refuses an active `epicnext-cms-app` CI-managed container.
|
||||
On a CI-managed host (either slot container running, or the nginx upstream
|
||||
pointing at a blue/green slot) the updater skips with exit 0, so a cron run
|
||||
that legitimately has nothing to do does not look like a failed deployment.
|
||||
|
||||
Logs: `logs/docker-update.log`. After cutover, a startup, image or HTTP check
|
||||
failure automatically recreates the CMS with its previous image and verifies its
|
||||
@@ -583,7 +596,7 @@ URLs look like `https://<hotel>/imaging/avatarimage?figure=hd-180-1.ch-210-66&im
|
||||
### Requirements (host)
|
||||
|
||||
- Docker (daemon with `build.network: host`, same as the CMS build — this host disables Docker iptables).
|
||||
- An nginx that serves `/gamedata/` (FigureData/FigureMap/EffectMap…) and `/gamedata/bundled` (`.nitro` assets) over HTTP so the renderer can fetch them. The compose file points at `host.docker.internal:8081`.
|
||||
- An nginx that serves `/gamedata/` (FigureData/FigureMap/EffectMap…) and `/gamedata/bundled` (furniture bundle assets — `.hab` by default, the extension imports write and this deployment's client requests, plus any legacy `.nitro` still on disk; choose the written extension with `FURNI_BUNDLE_EXT`) over HTTP so the renderer can fetch them. The compose file points at `host.docker.internal:8081`.
|
||||
|
||||
### Configuration — `/docker/Polaris-imager/.env`
|
||||
|
||||
@@ -626,9 +639,20 @@ A production issue that this section documents: **the `avatar-imaging-pixinode`
|
||||
|
||||
Since then the CMS-side avatar proxies (`/api/imaging/avatar` and `/imaging`) are resilient: when the configured upstream (the Polaris container on `8082`) fails or times out, the request is retried against Habbo's public renderer (`https://www.habbo.com/habbo-imaging/avatarimage`), with the `effect` parameter stripped and `img_format` forced to `png`, since the public renderer supports neither. The response is then marked with `X-Imager-Source: primary|fallback` and a shorter `Cache-Control` TTL when served from fallback, so the configured imager is retried soon instead of being masked for hours. No fallback is attempted when the configured upstream already is the Habbo public renderer. As a last line of defence every avatar `<img>` on the site falls back to a grayscale silhouette placeholder instead of a broken-image glyph.
|
||||
|
||||
### Figure string validation (`src/app/api/imaging/avatar/route.ts`)
|
||||
### Figure string validation
|
||||
|
||||
The CMS-side route re-validates the incoming figure against `/^[a-z]{2}-\d+(-\d+)?(\.[a-z]{2}-\d+(-\d+)?)*$/i`. Real Habbo figure strings use `type-id-color` parts separated by dots (`hd-180-1.ch-210-66`), so each part allows an optional second number. Invalid input is rejected with `400` before any render happens.
|
||||
The URL builder (`src/lib/imager.ts`) and the proxy routes share one rule set
|
||||
(`src/lib/avatar-figure.ts`): a figure must look like `part-id` segments
|
||||
separated by dots (`hd-180-1.ch-210-66`). Some clients append extra segments the
|
||||
renderer still accepts (e.g. a trailing `.0`), so the pattern is deliberately
|
||||
loose; over-strict validation once turned valid avatars into 400s.
|
||||
|
||||
Before a URL is built, the stored look is normalised: anything the imager would
|
||||
reject — an empty part slot such as the legacy `hr-100-.hd-180-1` default, a
|
||||
path-traversal attempt, an over-long value — is replaced with
|
||||
`DEFAULT_AVATAR_FIGURE`. That stops legacy accounts from rendering broken
|
||||
avatars without weakening the route, which still answers `400` to a malformed
|
||||
figure (`src/app/api/imaging/avatar/route.ts`).
|
||||
|
||||
---
|
||||
|
||||
@@ -838,130 +862,76 @@ Open **DevOps → Anti-DDoS protection**
|
||||
|
||||
---
|
||||
|
||||
## Local CrowdSec Engine (opt-in)
|
||||
## Captcha (Cloudflare Turnstile)
|
||||
|
||||
The repository ships a self-contained CrowdSec engine that runs on the same
|
||||
Docker host. It runs `crowdsecurity/crowdsec:v1.8.1` in its own Compose project
|
||||
and exposes **LAPI only** on `127.0.0.1:18080`. When enabled, the app-layer
|
||||
anti-DDoS gate (`src/lib/crowdsec-local.ts`) asks the local LAPI per client IP
|
||||
(short-cached) and blocks `ban` / `captcha` decisions before its own rate
|
||||
buckets run. No reverse-proxy, Traefik, Cloudflare or firewall configuration is
|
||||
changed.
|
||||
The **register**, **login**, **forgot-password** and **resend-verification**
|
||||
forms can be protected by a captcha. **Cloudflare Turnstile** is the supported
|
||||
provider and is free; reCAPTCHA and hCaptcha are also wired up. If you only
|
||||
read one thing, read the precedence table below.
|
||||
|
||||
The engine boots in **LAPI-only mode** (`DISABLE_AGENT=true`): it does not
|
||||
consume the host Nginx access log and needs no outbound access to
|
||||
`crowdsec.net`, which is often blocked on hardened hosts. Combined with
|
||||
`DISABLE_ONLINE_API=true` (no CrowdSec Central API) the engine needs no account
|
||||
and no inbound internet — blocking comes from the imported blocklists
|
||||
(Step 4) and the app's own rate buckets. Re-enable the agent only on a host
|
||||
with outbound internet by removing `DISABLE_AGENT: "true"` from
|
||||
`deployment/crowdsec/compose.crowdsec.yml`.
|
||||
### Where the captcha is configured (and who wins)
|
||||
|
||||
### Step 1 — Enable the engine and register the bouncer
|
||||
There are two places, and the **admin panel always wins**:
|
||||
|
||||
```bash
|
||||
bash cms security
|
||||
```
|
||||
1. **Admin panel** — *Admin → CMS Settings → Captcha* (stored in the database).
|
||||
2. **Environment variables** — used **only** as a fallback when the admin panel
|
||||
has no value.
|
||||
|
||||
This generates `CROWDSEC_LAPI_API_KEY` (random 64 hex chars), writes the
|
||||
CrowdSec flags into `.env`, starts the engine and registers the `cms` bouncer
|
||||
against the local LAPI. The engine does **not** enroll into the CrowdSec
|
||||
Central API (`DISABLE_ONLINE_API=true`) and runs without the agent
|
||||
(`DISABLE_AGENT=true`), so it never phones home.
|
||||
Resolution order, highest first:
|
||||
|
||||
### Step 2 — Restart the CMS so it loads the bouncer credentials
|
||||
| Admin panel `captcha_provider` | Env `CAPTCHA_PROVIDER` | What happens |
|
||||
| --- | --- | --- |
|
||||
| `turnstile` | anything | **Turnstile** (panel wins) |
|
||||
| `none` (set explicitly) | `turnstile` | **Off** — an explicit panel value wins |
|
||||
| *empty / never set* | `turnstile` | **Turnstile** (env fallback) |
|
||||
| *empty / never set* | *empty / never set* | **Off** |
|
||||
|
||||
A CI-managed `epicnext-cms-app` picks the new `.env` values up on its next
|
||||
deployment. For a clone running via the updater:
|
||||
In short: **enable it in the admin panel and the env vars are ignored; leave the
|
||||
panel empty and the env vars take over.** An explicit `none` in the panel turns
|
||||
the captcha **off** even when `CAPTCHA_PROVIDER=turnstile` is set.
|
||||
|
||||
```bash
|
||||
bash cms update --skip-pull
|
||||
```
|
||||
### Option A — configure with environment variables (per deploy)
|
||||
|
||||
or restart the container directly (`docker compose restart cms`). Without the
|
||||
restart the gate has not loaded the LAPI URL/key yet.
|
||||
1. Create a Turnstile widget in the
|
||||
[Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/turnstile):
|
||||
**Add widget**, name it, and add the hostnames your hotel is served on
|
||||
(e.g. `epicnabbo.nl`, `www.epicnabbo.nl`). *Managed* mode is fine. Copy the
|
||||
**Site Key** and the **Secret Key**.
|
||||
2. Put them in your real `.env` (never commit this file):
|
||||
|
||||
### Step 3 — Verify
|
||||
```dotenv
|
||||
CAPTCHA_PROVIDER=turnstile
|
||||
TURNSTILE_SITE_KEY=<your site key> # public — safe to expose
|
||||
TURNSTILE_SECRET_KEY=<your secret key> # SECRET — server-side only
|
||||
```
|
||||
|
||||
```bash
|
||||
bash cms security status
|
||||
```
|
||||
- The **site key** is **public**: it is rendered into the page for the
|
||||
browser.
|
||||
- The **secret key** is **secret**: only the server uses it to call
|
||||
Cloudflare's `siteverify` endpoint. **Never commit it** — keep it in the
|
||||
host `.env`, or a **masked CI/CD variable** if you deploy from CI.
|
||||
3. Restart the container so the new env is picked up.
|
||||
|
||||
Expect `CROWDSEC_LOCAL_ENABLED=yes` and `LAPI health: OK (127.0.0.1:18080)`.
|
||||
In the admin panel, **DevOps → Anti-DDoS protection** shows live block
|
||||
statistics split per origin (`community` vs `local`).
|
||||
### Option B — configure in the admin panel
|
||||
|
||||
### Step 4 — Stop the engine again (optional)
|
||||
**Admin → CMS Settings → Captcha**: pick the provider and paste the site key and
|
||||
secret. This overrides the env vars and takes effect **without a restart**.
|
||||
|
||||
```bash
|
||||
bash cms security disable
|
||||
```
|
||||
### Verify
|
||||
|
||||
Stops the container and sets `CROWDSEC_LOCAL_ENABLED=false`. Volumes and the
|
||||
`.env` key are kept.
|
||||
- Open `/register`. When active, the Turnstile widget appears above the submit
|
||||
button.
|
||||
- The e2e suite can assert the widget contract:
|
||||
`E2E_EXPECT_CAPTCHA=turnstile pnpm test:e2e` (spec: `e2e/register.spec.ts`).
|
||||
|
||||
### Step 5 — Load external IP blocklists (optional)
|
||||
### Notes
|
||||
|
||||
The engine has no built-in lists, so provide your own via a one-shot sync
|
||||
(fetches the sources, replaces every previous `cscli-import` decision):
|
||||
|
||||
```bash
|
||||
bash cms security blocklists
|
||||
```
|
||||
|
||||
Defaults: Spamhaus DROP/EDROP, DShield, CINS, Greensnow, StopForumSpam,
|
||||
Binary Defense, blocklist.de, Emerging Threats, BruteForceBlocker, abuse.ch
|
||||
Feodo/SSLBL, Darklist, Botvrij, IPsum, Firehol ipsets and Tor exit nodes
|
||||
(26 sources). URLhaus was removed because its `text_online` feed lists URLs,
|
||||
not IPs; a malformed token in it could otherwise expand into a bogus
|
||||
huge CIDR. The validator only accepts whole-line bare IPs or proper CIDRs,
|
||||
enforces sane prefix bounds and drops reserved/private/loopback space, so a
|
||||
bad source entry can never block the origin or internal traffic. The largest
|
||||
commercial/crowdsourced lists (AbuseIPDB, MaxMind, Cisco Talos, AlienVault
|
||||
OTX) are not included because they require an account or API key; IPsum
|
||||
already aggregates ~30 additional feeds. No account is needed, but internet
|
||||
access is — only for fetching; detection and blocking remain local. Sync
|
||||
hourly as a cron job:
|
||||
|
||||
```bash
|
||||
bash cms security blocklists-install-cron
|
||||
```
|
||||
|
||||
Removal: `bash cms security blocklists-uninstall-cron`. Dry-run without
|
||||
touching LAPI: `bash cms security blocklists --dry-run`. Override the sources,
|
||||
duration, a combined cap or an allowlist in `.env`
|
||||
(`CROWDSEC_BLOCKLIST_SOURCES`, `CROWDSEC_BLOCKLIST_DURATION`,
|
||||
`CROWDSEC_BLOCKLIST_MAX_DECISIONS`, `CROWDSEC_BLOCKLIST_ALLOW`). Existing
|
||||
`cscli-import` decisions are replaced on every sync, so removed entries
|
||||
expire.
|
||||
|
||||
### Environment variables
|
||||
|
||||
| Variable | Default | Purpose |
|
||||
| ---------------------------- | ----------------------------- | ------------------------------------ |
|
||||
| `CROWDSEC_LOCAL_ENABLED` | `false` | Master switch for the local stack |
|
||||
| `CROWDSEC_LAPI_URL` | `http://127.0.0.1:18080` | LAPI endpoint (loopback only) |
|
||||
| `CROWDSEC_LAPI_PORT` | `18080` | Host port the engine maps to LAPI |
|
||||
| `CROWDSEC_LAPI_API_KEY` | — | Bouncer key; required when enabled |
|
||||
| `CROWDSEC_LAPI_TIMEOUT_MS` | `500` | Per-decision request timeout |
|
||||
| `CROWDSEC_LAPI_RETRY_MS` | `500` | Backoff before retrying LAPI |
|
||||
| `CROWDSEC_NGINX_LOG_DIR` | `/var/log/nginx` | Access-log directory for the engine |
|
||||
|
||||
### Notes and limitations
|
||||
|
||||
- Changing the port means updating `CROWDSEC_LAPI_PORT` **and**
|
||||
`CROWDSEC_LAPI_URL` together, then re-running `bash cms security`.
|
||||
- Rotate the key by editing `CROWDSEC_LAPI_API_KEY` in `.env`, running
|
||||
`bash cms security` again (re-registers the bouncer) and restarting the CMS.
|
||||
- The gate is **fail-closed at startup** when the feature is enabled without a
|
||||
key (startup aborts with a clear message). At runtime a LAPI network error
|
||||
**fails open** (traffic is allowed, decisions paused); a 403 from LAPI
|
||||
pauses local decisions for 5 minutes.
|
||||
- This bouncer is **application-layer**: it sheds known-bad IPs at the CMS
|
||||
process only. It does not drop traffic before the origin, does not protect
|
||||
other host ports/services, and depends on the client IP being trustworthy at
|
||||
the ingress. Keep the upstream protections (Cloudflare IP rules, proxy rate
|
||||
limits) for defense before the origin.
|
||||
- **Fail-closed**: if the provider is Turnstile but no secret key is available,
|
||||
verification fails and the form is blocked — it never silently lets bots in.
|
||||
- The Content-Security-Policy already allows `https://challenges.cloudflare.com`
|
||||
(`src/lib/csp.ts`), so no header changes are needed.
|
||||
- Turnstile does **not** require the Cloudflare proxy (orange cloud); it works
|
||||
independently of the anti-DDoS layer above.
|
||||
|
||||
---
|
||||
|
||||
@@ -1001,6 +971,25 @@ that the CI deploy path deliberately uses `docker run` rather than compose, so
|
||||
the resource limits are declared in **both** places — limits that only existed
|
||||
in compose would never apply to a real release.
|
||||
|
||||
### Compose on a CI host
|
||||
|
||||
Compose and CI both want port 3002, so only one of them can own a host. A stray
|
||||
compose replica (`docker compose up`, or the daily `scripts/docker-update.sh`
|
||||
cron) parked an `epicnext-cms` container on the blue slot while nginx served the
|
||||
green slot, and every later release stopped on "Port 3002 is already in use" —
|
||||
after the build, the migrations and the browser gate. Two guards now prevent
|
||||
that:
|
||||
|
||||
- `scripts/docker-update.sh` refuses to run on a CI host. It used to test only
|
||||
`epicnext-cms-app`, but after a cutover to the green slot that container is
|
||||
stopped and deleted, so the guard stopped firing while the host stayed
|
||||
CI-managed. It now checks both slot containers and the nginx upstream, and
|
||||
reports the skip with exit 0 instead of failing the cron.
|
||||
- `ci-deploy.sh` retires a compose replica of *this* checkout
|
||||
(`com.docker.compose.project.config_files`) from the candidate port before
|
||||
starting the candidate — but never a slot container, and never the port nginx
|
||||
currently serves. Anything else still fails loudly in `assert_port_free`.
|
||||
|
||||
### The nginx upstream is the switch
|
||||
|
||||
nginx does not know about container names; it reads a plain list of backends from
|
||||
@@ -1059,15 +1048,112 @@ branch guard inside `ci-deploy.sh` only accepts `main`/`master`.
|
||||
|
||||
---
|
||||
|
||||
## Memory-capped commands
|
||||
|
||||
This host runs with `vm.overcommit_memory=0` **and no swap**. When a process
|
||||
asks for more memory than is free, the kernel does not wait — it calls the
|
||||
OOM-killer immediately, and it picks its victim across the **whole machine**,
|
||||
not just the offending process. An uncapped build does not merely fail: it can
|
||||
take the MariaDB process, nginx and the live release down with it.
|
||||
|
||||
Every heavy command therefore runs inside its own cgroup with a hard
|
||||
`MemoryMax`, via `scripts/with-memory-cap.sh`. If the build outgrows its
|
||||
ceiling, only that cgroup is killed — the build fails, the site keeps serving.
|
||||
|
||||
The systemd backend also sets `MemoryHigh` (95% of the ceiling) so the kernel
|
||||
reclaims and throttles **before** the hard limit, and it clamps the ceiling to
|
||||
`MemAvailable - reserve` (`CMS_MEMORY_CAP_RESERVE`, default `4g`). That stops a
|
||||
cap larger than the free memory from dragging the whole host into the global
|
||||
OOM-killer. `MemorySwapMax=0` is set explicitly (this host has no swap). If
|
||||
fewer than 1 GB would remain, the script refuses to start.
|
||||
|
||||
| Script | Ceiling | Covers |
|
||||
| --------------------- | ------- | --------------------------------------- |
|
||||
| `pnpm dev` | 8 GB | Dev server |
|
||||
| `pnpm build` | 10 GB | Production build |
|
||||
| `pnpm analyze` | 10 GB | Build + bundle-size report |
|
||||
| `pnpm test` | 8 GB | Vitest |
|
||||
| `pnpm test:coverage` | 8 GB | Vitest with coverage |
|
||||
| `pnpm test:ui` | 8 GB | Playwright |
|
||||
| `pnpm test:e2e` | 8 GB | Playwright |
|
||||
| `pnpm test:e2e:quality` | 8 GB | Playwright (advisory) |
|
||||
| `pnpm test:integration` | 8 GB | Vitest integration config |
|
||||
| `pnpm typecheck` | 6 GB | `tsc --noEmit` |
|
||||
|
||||
### Running the builder by hand
|
||||
|
||||
```bash
|
||||
npx next build # ✗ refused before it allocates anything
|
||||
```
|
||||
|
||||
`next build` loads `next.config.ts`, which **refuses any production build that
|
||||
is not running under the memory cap**. This closes the one hole the `pnpm`
|
||||
scripts leave open: invoking the builder directly — from a terminal, an IDE, or
|
||||
an automated agent — would otherwise bypass the cgroup entirely and go
|
||||
unbounded.
|
||||
|
||||
The refusal looks like this:
|
||||
|
||||
```
|
||||
Error: Refusing to run an uncapped production build.
|
||||
|
||||
On this host an unbounded `next build` gets OOM-killed by the kernel,
|
||||
and the killer may take the database, nginx or the live release with it.
|
||||
|
||||
Use the capped build instead:
|
||||
pnpm build
|
||||
```
|
||||
|
||||
Fix: use `pnpm build`. If you are genuinely inside an isolated environment
|
||||
where the container *is* the boundary (the Docker build, a CI runner), set
|
||||
`CMS_MEMORY_CAPPED=1` to opt out deliberately.
|
||||
|
||||
### Backends
|
||||
|
||||
`scripts/with-memory-cap.sh` picks its mechanism automatically:
|
||||
|
||||
| `CMS_MEMORY_CAP_BACKEND` | Mechanism | Notes |
|
||||
| ------------------------ | ------------------------------------------- | ------------------------------------------------------- |
|
||||
| `auto` *(default)* | systemd cgroup `MemoryMax` | Real RSS bound over the whole process tree. Used here. |
|
||||
| `ulimit` | `ulimit -v`, per process | Virtual address space, **not** RSS. Fallback only. |
|
||||
| `none` | None — warning only | Docker build and GitLab runner, each already isolated. |
|
||||
|
||||
On a host **without** systemd and without `CMS_MEMORY_CAP_BACKEND=none`, the
|
||||
script refuses to run rather than proceeding unbounded.
|
||||
|
||||
Tunables for the systemd backend: `CMS_MEMORY_CAP_RESERVE` (default `4g`) is
|
||||
the headroom left for everything else on the machine; the effective ceiling is
|
||||
`min(requested, MemAvailable - reserve)`.
|
||||
|
||||
> Do not "fix" a cap failure by lowering `--max-old-space-size` or by raising
|
||||
> `CMS_MEMORY_CAP_VIRTUAL` (the default is `40g` on purpose). A `ulimit -v` of
|
||||
> 10g makes V8 clamp its own heap to ~2.25 GB and webpack dies with
|
||||
> `std::bad_alloc`. Measure first; raise the ceiling deliberately.
|
||||
|
||||
---
|
||||
|
||||
## Scripts
|
||||
|
||||
| Command | Description |
|
||||
| ------------------------- | -------------------------------------------------- |
|
||||
| `pnpm dev` | Start development server (hot reload) |
|
||||
| `pnpm build` | Production build |
|
||||
| `pnpm build` | Production build (memory-capped) |
|
||||
| `pnpm start` | Start production server |
|
||||
| `pnpm typecheck` | Run TypeScript type checking |
|
||||
| `pnpm test` | Run all tests (Vitest) |
|
||||
| `pnpm test:coverage` | Run all tests with coverage thresholds enforced |
|
||||
| `pnpm test:ui` | Playwright UI tests (`playwright.ui.config.ts`) |
|
||||
| `pnpm test:ui:update` | Playwright UI tests, updating snapshots |
|
||||
| `pnpm test:e2e` | Playwright end-to-end tests (blocking deploy gate) |
|
||||
| `pnpm test:e2e:quality` | Advisory content/latency checks (non-blocking) |
|
||||
| `pnpm test:integration` | Integration tests (own Vitest config) |
|
||||
| `pnpm test:housekeeping` | Housekeeping feature tests |
|
||||
| `pnpm lint` | Lint and format check (Biome) |
|
||||
| `pnpm biome:lint` | Alias of `pnpm lint` |
|
||||
| `pnpm format` | Format files in place (Biome) |
|
||||
| `pnpm toolchain:check` | Verify the Node toolchain matches `.nvmrc` |
|
||||
| `pnpm i18n:check` | Audit CMS translation coverage |
|
||||
| `pnpm deps:audit` | Audit dependencies for high-severity advisories |
|
||||
| `pnpm db:migrate` | Apply pending SQL migrations |
|
||||
| `pnpm db:migrate:status` | Show migration status |
|
||||
| `pnpm db:schema:generate` | Regen `src/db/schema.ts` from prior schema + live DB |
|
||||
@@ -1075,13 +1161,19 @@ branch guard inside `ci-deploy.sh` only accepts `main`/`master`.
|
||||
| `pnpm db:studio` | Drizzle Studio (dev) |
|
||||
| `pnpm db:introspect` | drizzle-kit introspect (draft) |
|
||||
| `pnpm db:bulk` | Batch-import >50 MB JSON via `scripts/bulk-import-json.ts` |
|
||||
| `pnpm catalog:sync` | Sync the catalog against a clone source (interactive) |
|
||||
| `pnpm db:up` / `pnpm db:down` | Start / stop the `mariadb-turbo` container |
|
||||
| `pnpm gamedata:compress` | Pre-compress large gamedata JSON to `.gz` (gzip_static) |
|
||||
| `pnpm analyze` | Build + open bundle analyzer |
|
||||
| `pnpm analyze` | Build + report per-route bundle sizes |
|
||||
| `pnpm performance:report` | Re-render the performance report from a build |
|
||||
| `pnpm jobs:worker` | Start background task worker |
|
||||
| `pnpm biome:check` | Lint and format code |
|
||||
| `pnpm assets:editor` | Copy TinyMCE editor assets into `public/` |
|
||||
|
||||
> Replace `pnpm` with `npm run` or `yarn` for other package managers.
|
||||
>
|
||||
> The heavy ones run memory-capped — see
|
||||
> [Memory-capped commands](#memory-capped-commands). A production `next build`
|
||||
> run outside the wrapper is refused rather than executed unbounded.
|
||||
|
||||
---
|
||||
|
||||
@@ -1175,6 +1267,13 @@ pnpm jobs:worker # or: npm run jobs:worker / yarn jobs:worker
|
||||
|
||||
Optional OpenAI-powered moderation for comments and guestbook posts. Set `OPENAI_API_KEY`.
|
||||
|
||||
### Captcha — Cloudflare Turnstile
|
||||
|
||||
Protect the register, login, forgot-password and resend-verification forms.
|
||||
Set `CAPTCHA_PROVIDER=turnstile` plus the Turnstile keys in `.env`, or configure
|
||||
it in **Admin → CMS Settings**. Full setup, precedence and verification:
|
||||
[Captcha (Cloudflare Turnstile)](#captcha-cloudflare-turnstile).
|
||||
|
||||
### Byparr (Cloudflare Bypass)
|
||||
|
||||
Some clone sources are protected by Cloudflare. Byparr (a FlareSolverr successor) solves these challenges automatically.
|
||||
@@ -1206,10 +1305,29 @@ The CMS automatically translates furniture names and descriptions to **13 langua
|
||||
pnpm dev # Start with hot reload
|
||||
pnpm typecheck # Type check all files
|
||||
pnpm test # Run test suite
|
||||
pnpm analyze # Build and analyze bundle sizes
|
||||
pnpm analyze # Build and report per-route bundle sizes
|
||||
pnpm biome:check # Lint and format
|
||||
```
|
||||
|
||||
### End-to-end tests
|
||||
|
||||
`pnpm test:e2e` is the **blocking deploy gate**. It only contains checks the CMS
|
||||
can guarantee on its own (anonymous redirects, the register form fields and the
|
||||
captcha DOM contract, cache-control headers), so a green run means a code deploy
|
||||
is safe.
|
||||
|
||||
Checks that depend on **live content or data** — a missing upload or theme asset
|
||||
(`/help` article images), a legacy malformed avatar figure, warm-page latency —
|
||||
live in a separate advisory suite:
|
||||
|
||||
```bash
|
||||
pnpm test:e2e:quality # playwright.quality.config.ts, never blocks a deploy
|
||||
```
|
||||
|
||||
It is run against the public site as a `continue-on-error` CI job, so it still
|
||||
surfaces a real content problem without turning the pipeline red. Point either
|
||||
suite at any running CMS with `PLAYWRIGHT_BASE_URL`.
|
||||
|
||||
### Contributing
|
||||
|
||||
1. Ensure typecheck and tests pass: `pnpm typecheck && pnpm test`
|
||||
@@ -1223,3 +1341,5 @@ pnpm biome:check # Lint and format
|
||||
## License
|
||||
|
||||
CC BY-NC-SA 4.0. See `LICENSE` for details.
|
||||
test line
|
||||
// CI monitor test 1791828076
|
||||
+24
@@ -38,6 +38,30 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"includes": [
|
||||
"src/components/admin/catalog-manager/sortable-tree.tsx",
|
||||
"src/components/admin/studio/organize-imports-dialog/mall-helpers.tsx",
|
||||
"src/app/admin/import/furni/nitro-editor-dialog.tsx"
|
||||
],
|
||||
"linter": {
|
||||
"rules": {
|
||||
"suspicious": {
|
||||
"noArrayIndexKey": "off"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"includes": ["src/components/admin/catalog-manager/sortable-tree.tsx"],
|
||||
"linter": {
|
||||
"rules": {
|
||||
"correctness": {
|
||||
"useExhaustiveDependencies": "off"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"css": {
|
||||
|
||||
@@ -4,7 +4,6 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
case "${1:-help}" in
|
||||
install) shift; exec bash "$DIR/scripts/docker-install.sh" "$@" ;;
|
||||
update) shift; exec bash "$DIR/scripts/docker-update.sh" "$@" ;;
|
||||
security) shift; exec bash "$DIR/scripts/crowdsec-setup.sh" "$@" ;;
|
||||
help|--help|-h) printf '%s\n' 'bash cms install Configure and install on a Linux Docker host' 'bash cms update Update using saved settings; --skip-pull uses checked-out release' 'bash cms security Configure the opt-in local CrowdSec stack (enable|status|disable|blocklists)' ;;
|
||||
*) echo "Unknown command. Use: bash cms install | update | security" >&2; exit 1 ;;
|
||||
help|--help|-h) printf '%s\n' 'bash cms install Configure and install on a Linux Docker host' 'bash cms update Update using saved settings; --skip-pull uses checked-out release' ;;
|
||||
*) echo "Unknown command. Use: bash cms install | update" >&2; exit 1 ;;
|
||||
esac
|
||||
@@ -1,4 +0,0 @@
|
||||
filenames:
|
||||
- /var/log/nginx/access.log
|
||||
labels:
|
||||
type: nginx
|
||||
@@ -1,41 +0,0 @@
|
||||
services:
|
||||
crowdsec:
|
||||
image: crowdsecurity/crowdsec:${CROWDSEC_VERSION:-v1.8.1}
|
||||
container_name: epicnext-crowdsec
|
||||
restart: unless-stopped
|
||||
profiles: ["security"]
|
||||
environment:
|
||||
DISABLE_AGENT: "true"
|
||||
BOUNCER_KEY_cms: ${CROWDSEC_LAPI_API_KEY:?CROWDSEC_LAPI_API_KEY must be set}
|
||||
DISABLE_ONLINE_API: "true"
|
||||
GID: "${CROWDSEC_GID:-0}"
|
||||
TZ: "${TZ:-UTC}"
|
||||
ports:
|
||||
- "${CROWDSEC_LAPI_BIND_HOST:-127.0.0.1}:${CROWDSEC_LAPI_PORT:-18080}:8080"
|
||||
volumes:
|
||||
- ./acquis.d:/etc/crowdsec/acquis.d:ro
|
||||
- ${CROWDSEC_NGINX_LOG_DIR:-/var/log/nginx}:/var/log/nginx:ro
|
||||
- crowdsec-config:/etc/crowdsec
|
||||
- crowdsec-data:/var/lib/crowdsec/data
|
||||
security_opt:
|
||||
- no-new-privileges:true
|
||||
pids_limit: 256
|
||||
logging:
|
||||
driver: json-file
|
||||
options:
|
||||
max-size: "10m"
|
||||
max-file: "3"
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD-SHELL",
|
||||
"wget -q -O - http://127.0.0.1:8080/health >/dev/null 2>&1",
|
||||
]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
|
||||
volumes:
|
||||
crowdsec-config:
|
||||
crowdsec-data:
|
||||
@@ -78,6 +78,23 @@ map "$cms_cc_class|$cms_skip_cache" $cms_public_cc {
|
||||
"~^3\|0" "public, max-age=300, s-maxage=300, stale-while-revalidate=600";
|
||||
}
|
||||
|
||||
# Bestandsnaam van een furni-icon, opgehaald uit de URL. De locatie voor
|
||||
# /swf/dcr/hof_furni/icons/ heeft die nodig om hetzelfde bestand bij de
|
||||
# gamedata-boom op te kunnen vragen. Leeg laten voor elke andere URL, zodat
|
||||
# niets anders deze waarde per ongeluk gebruikt.
|
||||
map $uri $furni_icon_file {
|
||||
~^/swf/dcr/hof_furni/icons/(?<icon_file>.+)$ $icon_file;
|
||||
default "";
|
||||
}
|
||||
|
||||
# Cache-Control per status voor het CMS-valrimpeltje. Een 404 mag nooit
|
||||
# gecacht worden (zie @gamedata_missing hieronder), dus die krijgt `no-store`
|
||||
# in plaats van de icon-TTL.
|
||||
map $upstream_status $furni_icon_cc {
|
||||
200 "public, max-age=604800, stale-while-revalidate=2592000";
|
||||
default "no-store";
|
||||
}
|
||||
|
||||
# ─── Mime fix ───
|
||||
types {
|
||||
application/json jsonc;
|
||||
@@ -162,6 +179,22 @@ server {
|
||||
ssl_early_data on;
|
||||
add_header Alt-Svc 'h3=":9443"; ma=86400' always;
|
||||
|
||||
# Resuming a session skips the full handshake, which is most of the cost of
|
||||
# a TLS connection. Without this nginx performs no session resumption at all:
|
||||
# every visitor paid a full handshake on every request. 50M shared sessions
|
||||
# is roughly 1GB at the default 20-byte key id plus overhead.
|
||||
ssl_session_cache shared:CMS_TLS:50m;
|
||||
ssl_session_timeout 1d;
|
||||
ssl_session_tickets off;
|
||||
|
||||
# `index index.html` without a `root` left nginx resolving every
|
||||
# try_files/$uri against the compiled-in default /etc/nginx/html. The
|
||||
# /robots.txt and /favicon.ico probes then stat() a path the worker cannot
|
||||
# traverse, and because a failed stat is logged at crit the error log filled
|
||||
# with 149 crit lines per scan. Pointing root at the CMS document root makes
|
||||
# the same probe a plain 404, which log_not_found already suppresses.
|
||||
root /var/www/html;
|
||||
|
||||
index index.html;
|
||||
|
||||
# ─── Security Headers ───
|
||||
@@ -360,14 +393,113 @@ server {
|
||||
return 404;
|
||||
}
|
||||
|
||||
# ─── Furni-icons: gamedata-boom eerst, CMS-boom als valrimpeltje ───
|
||||
#
|
||||
# De CMS bouwde zijn icon-URL's altijd vanaf /swf/dcr/hof_furni/icons/,
|
||||
# maar de catalogus komt uit /var/www/Gamedata/icons:
|
||||
# - van de 16.263 classnames in items_base staat er 15.338 hier onder de
|
||||
# "veilige" naam (`highscore_perteam_1_icon.png`), tegen 11.267 in
|
||||
# public/swf/dcr/hof_furni/icons;
|
||||
# - de swf-boom houdt kleurvarianten bovendien vast met een letterlijke
|
||||
# `*` in de bestandsnaam (`highscore_perteam*1_icon.png`), dus elke
|
||||
# aanvraag met de veilige naam mist;
|
||||
# - en `hof.furni.url` wijst de Nitro-client al naar deze boom.
|
||||
# Resultaat was een 404 voor `highscore_perteam_1_icon.png` en duizenden
|
||||
# andere, terwijl `/gamedata/icons/` ze wél heeft.
|
||||
#
|
||||
# Deze locatie leest de gamedata-boom rechtstreeks van schijf — nginx heeft
|
||||
# er leesrechten op en het is de boom die de client ook gebruikt. Wat daar
|
||||
# niet staat wordt doorgestuurd naar de CMS, die uit public/ serveert,
|
||||
# zodat niets wat nu al laadde stopt met laden.
|
||||
#
|
||||
# Een echte miss is een no-store 404, nooit een gecachte: `add_header`
|
||||
# zonder `always` zegt niets over een 404, en zonder de expliciete handler
|
||||
# hieronder neemt Cloudflare de zone-TTL (1 jaar) over. Zelfde les als bij
|
||||
# @gamedata_missing hierboven.
|
||||
location ^~ /swf/dcr/hof_furni/icons/ {
|
||||
alias /var/www/Gamedata/icons/;
|
||||
error_page 404 = @furni_icon_from_cms;
|
||||
# 403 = het pad valt op een map, dus `alias` eindigt op een directory.
|
||||
# Dat is geen icon, dus dezelfde route als een miss.
|
||||
error_page 403 = @furni_icon_missing;
|
||||
|
||||
# Geen limit_req: zelfde reden als /gamedata/icons/. Een kamerladen
|
||||
# vuurt honderden icons in één burst af.
|
||||
add_header Cache-Control "public, max-age=604800, stale-while-revalidate=2592000";
|
||||
add_header Cache-Tag "cms-furni-icons";
|
||||
access_log off;
|
||||
|
||||
add_header Access-Control-Allow-Origin $http_origin always;
|
||||
add_header Access-Control-Allow-Methods "GET, OPTIONS" always;
|
||||
if ($cors_headers) {
|
||||
add_header Access-Control-Max-Age 1728000;
|
||||
add_header Content-Type "text/plain; charset=utf-8";
|
||||
return 204;
|
||||
}
|
||||
}
|
||||
|
||||
# Niet in de gamedata-boom: val terug op public/swf/dcr/hof_furni/icons/,
|
||||
# waar de CMS naartoe importeert. Het pad wordt hier opnieuw opgebouwd
|
||||
# omdat een named location geen prefix van `$uri` afstropt.
|
||||
#
|
||||
# De Cache-Control komt uit `$furni_icon_cc` (status-afhankelijk) in plaats
|
||||
# van uit een tweede `error_page`: nginx negeert `error_page` die binnen
|
||||
# een named location staat die zelf via `error_page` bereikt is, dus een
|
||||
# geketende 404-handler bestaat hier niet.
|
||||
location @furni_icon_from_cms {
|
||||
internal;
|
||||
proxy_pass http://cms_app/swf/dcr/hof_furni/icons/$furni_icon_file;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header Connection "";
|
||||
proxy_intercept_errors on;
|
||||
|
||||
# `=404` (geen URI) vervangt de body door nginx' eigen foutpagina. Next
|
||||
# stuurt voor een miss een volledige HTML-pagina van ~300 KB mee, en een
|
||||
# gebroken icon hoeft geen 300 KB aan markup op te halen. Dit is geen
|
||||
# interne redirect, dus de `add_header` hieronder blijven gelden.
|
||||
error_page 404 =404;
|
||||
|
||||
# `proxy_hide_header` haalt de Cache-Control van de CMS weg, anders
|
||||
# staan er twee in één antwoord (Next stuurt voor /swf/** een
|
||||
# `public, max-age=604800`, `location /` elders nog een
|
||||
# `private, no-cache`). Eén header per antwoord.
|
||||
proxy_hide_header Cache-Control;
|
||||
add_header Cache-Control $furni_icon_cc always;
|
||||
add_header Cache-Tag "cms-furni-icons";
|
||||
access_log off;
|
||||
}
|
||||
|
||||
location @furni_icon_missing {
|
||||
add_header Cache-Control "no-store" always;
|
||||
add_header Cache-Tag "cms-furni-icons" always;
|
||||
add_header Access-Control-Allow-Origin $http_origin always;
|
||||
add_header Access-Control-Allow-Methods "GET, OPTIONS" always;
|
||||
access_log off;
|
||||
return 404;
|
||||
}
|
||||
|
||||
location /camera/ {
|
||||
alias /var/www/Camera/;
|
||||
add_header Cache-Control "public, max-age=31536000, immutable";
|
||||
add_header Cache-Tag "cms-camera";
|
||||
}
|
||||
|
||||
# robots.txt is generated by the CMS (src/app/robots.ts, force-dynamic
|
||||
# because it needs APP_URL) and sitemap.xml points crawlers at it. This
|
||||
# location used to answer from disk with try_files, which made it a
|
||||
# guaranteed 404: the file does not exist in public/, so crawlers were told
|
||||
# to obey a robots.txt they could never read. Proxy it like the route it
|
||||
# actually is. favicon.ico below stays on disk — log_not_found already
|
||||
# keeps its miss quiet.
|
||||
location = /robots.txt {
|
||||
access_log off;
|
||||
proxy_pass http://cms_app;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
|
||||
location = /favicon.ico { expires 1y; access_log off; log_not_found off; try_files $uri =404; }
|
||||
location = /robots.txt { expires 1d; access_log off; log_not_found off; try_files $uri =404; }
|
||||
|
||||
# ─── Static Next.js Assets ───
|
||||
location /_next/static/ {
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
[Unit]
|
||||
# The scheduled-job worker (scheduled articles, catalog export, backups, disk
|
||||
# and health probes). This is NOT optional: a web process alone does not
|
||||
# establish that scheduled work runs. The CMS reports it as a failed
|
||||
# diagnostic row when the Redis heartbeat at cms:jobs-worker:heartbeat is
|
||||
# missing, which is exactly what happened while nothing supervised this.
|
||||
#
|
||||
# It runs on the host rather than in a container on purpose: the schedule
|
||||
# shells out to mysqldump, df and docker, none of which exist in the CMS image,
|
||||
# and it must survive CMS deploys (a container is replaced on every release).
|
||||
Description=AtomNext CMS scheduled-job worker
|
||||
Documentation=https://gitlab.epicnabbo.nl/remco/EpicNext-Cms
|
||||
After=network-online.target docker.service mariadb.service
|
||||
Wants=network-online.target
|
||||
# Start ordering only; the worker tolerates the database being briefly absent
|
||||
# and retries, so do not make it hard-fail when mariadb is slow to boot.
|
||||
Wants=docker.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
WorkingDirectory=/var/www/atom-nexst
|
||||
Environment=NODE_ENV=production
|
||||
ExecStart=/usr/bin/node --conditions=react-server --import tsx scripts/jobs-worker.ts
|
||||
# The worker's own catch-all logs and exits 1 on a fatal error, so a restart is
|
||||
# always wanted. 10s backoff stops a persistent misconfiguration (missing .env,
|
||||
# bad DATABASE_URL) from spinning.
|
||||
Restart=always
|
||||
RestartSec=10
|
||||
# Give a crashed job time to finish its DB transaction before the next start,
|
||||
# otherwise a mid-transaction kill can loop on the same failure.
|
||||
TimeoutStopSec=30
|
||||
KillSignal=SIGTERM
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
SyslogIdentifier=cms-jobs-worker
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,19 @@
|
||||
[Service]
|
||||
# systemd's default is 1024:524288, i.e. a *soft* LimitNOFILE of 1024. nginx
|
||||
# inherits that soft limit, so worker_connections 2048 could not actually be
|
||||
# reached and every start logged:
|
||||
# "2048 worker_connections exceed open file resource limit: 1024"
|
||||
# Raise both soft and hard to 65536 so the master's rlimit covers
|
||||
# worker_connections before nginx is even started.
|
||||
LimitNOFILE=65536
|
||||
|
||||
# The packaged unit ships Restart=no, so a crashed or OOM-killed nginx stayed
|
||||
# down until someone noticed. nginx is the only thing serving the site, so it
|
||||
# must come back on its own. `on-failure` restarts only abnormal exits, which
|
||||
# keeps an operator-initiated `systemctl stop` from being undone.
|
||||
Restart=on-failure
|
||||
RestartSec=2
|
||||
|
||||
# Give in-flight requests time to drain on stop/reload instead of severing
|
||||
# keepalive connections and long-polling SSE streams mid-response.
|
||||
TimeoutStopSec=30
|
||||
+10
-40
@@ -1,30 +1,22 @@
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
# Next.js CMS — blue/green
|
||||
#
|
||||
# De app draait met `network_mode: host`, dus een replica neemt een host-poort in
|
||||
# plaats van een gedeelde docker-poort. Daarom twee expliciete services in plaats
|
||||
# van `docker compose up --scale cms=2`: die zou op poort 3002 botsen.
|
||||
#
|
||||
# `deploy.sh` start een release op de vrije poort, wacht op /api/health, schrijft
|
||||
# daarna /etc/nginx/snippets/cms_upstream_servers.conf en herlaadt nginx. Pas dan
|
||||
# wordt de oude replica gestopt. De hele release is dus zero-downtime: faalt de
|
||||
# nieuwe replica, dan blijft de oude gewoon draaien.
|
||||
#
|
||||
# De YAML-anchor houdt beide replicas identiek. Wil je ze bewust uit elkaar
|
||||
# halen (bv. één release canary-en), verwijder dan `<<: *cms` en vul de
|
||||
# afwijkende velden opnieuw in.
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
x-cms: &cms
|
||||
image: epicnext-cms:${CMS_RELEASE:-local}
|
||||
# No `network: host` on the build. BuildKit (v0.26, Docker 29) refuses to grant
|
||||
# host networking unless every caller passes --allow=network.host, and
|
||||
# `docker compose build` has no such flag — so asking for it here turned every
|
||||
# rebuild into an immediate "additional privileges requested" failure, which
|
||||
# left the previous release serving traffic. The build only needs outbound
|
||||
# internet (apk, pnpm, next/font/google), which the default bridge provides.
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
args:
|
||||
NEXT_DEPLOYMENT_ID: ${CMS_RELEASE:-unknown}
|
||||
network: host
|
||||
# Runtime host networking IS required: blue/green needs per-release host ports
|
||||
# (3002/3003) and nginx reaches the slot over 127.0.0.1.
|
||||
network_mode: host
|
||||
# 15s: Next moet een lopend request nog netjes kunnen afronden voordat SIGKILL
|
||||
# volgt. Met 10s werden streams en imports afgekapt.
|
||||
stop_grace_period: 15s
|
||||
restart: unless-stopped
|
||||
env_file:
|
||||
@@ -36,20 +28,11 @@ x-cms: &cms
|
||||
- /var/www/Gamedata:/var/www/Gamedata
|
||||
|
||||
# ── Resource limits ──
|
||||
# De limieten waren eerder weggehaald ("Next mag onbeperkt presteren"). Op een
|
||||
# gedeelde host is juist dat gevaarlijk: één geheugenlek vult dan de hele
|
||||
# machine en MariaDB + nginx + Traefik gaan er allemaal onderuit. 4 GiB met
|
||||
# 1 GiB swap geeft de V8-heap ruimte om zich te organiseren voor hij hard wordt
|
||||
# afgesneden, maar houdt de schade begrensd. 2 CPU laat drie keer zoveel
|
||||
# achtergrondwerk toe als de cores, zodat de 6 cores van deze host niet
|
||||
# volledig door twee replicas worden opgeëist.
|
||||
mem_limit: 4g
|
||||
memswap_limit: 5g
|
||||
mem_limit: 6g
|
||||
memswap_limit: 7g
|
||||
cpus: 2.0
|
||||
pids_limit: 512
|
||||
|
||||
# Leest de poort uit de eigen omgeving, dus dezelfde healthcheck werkt voor
|
||||
# 3002 én 3003 zonder dat deze tweemaal in de compose hoeft te staan.
|
||||
healthcheck:
|
||||
test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:'+(process.env.PORT||'3002')+'/api/health').then(r=>{process.exit(r.ok?0:1)}).catch(()=>process.exit(1))"]
|
||||
interval: 15s
|
||||
@@ -58,7 +41,6 @@ x-cms: &cms
|
||||
start_period: 40s
|
||||
|
||||
services:
|
||||
# Blauwe replica: host-poort 3002.
|
||||
cms:
|
||||
<<: *cms
|
||||
container_name: epicnext-cms
|
||||
@@ -66,8 +48,6 @@ services:
|
||||
- HOSTNAME=0.0.0.0
|
||||
- PORT=3002
|
||||
|
||||
# Groene replica: host-poort 3003. Meestal uitgeschakeld; alleen tijdens een
|
||||
# release gestart, totdat nginx hem in de upstream-lijst heeft overgenomen.
|
||||
cms-green:
|
||||
<<: *cms
|
||||
container_name: epicnext-cms-green
|
||||
@@ -76,7 +56,6 @@ services:
|
||||
- HOSTNAME=0.0.0.0
|
||||
- PORT=3003
|
||||
|
||||
# ── Byparr (Cloudflare bypass for clone sources) ──
|
||||
byparr:
|
||||
image: ghcr.io/thephaseless/byparr:latest
|
||||
container_name: byparr
|
||||
@@ -84,19 +63,10 @@ services:
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
- LOG_LEVEL=INFO
|
||||
|
||||
# Resource limits verwijderd: Headless Chrome heeft bij zware pagina-scrapes
|
||||
# soms tijdelijk meer dan 1 GB RAM nodig. Nu krijgt hij alle ruimte.
|
||||
pids_limit: 256
|
||||
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "http://localhost:8191/health"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
|
||||
# De database draait niet meer in Docker. `mariadb-turbo` is verwijderd: de
|
||||
# service is nooit gestart, de volume bestond niet, en de echte MariaDB draait
|
||||
# al als host-proces op 127.0.0.1:3306. De optimalisatie-vlaggen daar stonden
|
||||
# dus al langer niets meer in beheer.
|
||||
@@ -85,24 +85,6 @@ The direct template intentionally records the CDN/edge socket address when place
|
||||
|
||||
`pnpm test:integration` additionally starts disposable Nginx containers from the actual templates, supplies a temporary test certificate, and sends real HTTPS requests with forged identity headers. It checks direct-mode replacement even with an inherited real-IP rule, rejection of untrusted peers, and acceptance through an explicitly trusted peer. This requires Docker Engine and the OpenSSL CLI and does not read deployment credentials. The templates must still pass `nginx -t` on the intended host after its hostname/certificate substitution, then the listener and trusted-header checks above; the disposable fixture cannot certify that host or its firewall.
|
||||
|
||||
## Opt-in: CrowdSec on the same Docker host
|
||||
|
||||
A self-contained CrowdSec engine ships in `deployment/crowdsec`. It runs `crowdsecurity/crowdsec:v1.8.1` in its own Compose project in **LAPI-only mode** (`DISABLE_AGENT=true`) and exposes LAPI only on `127.0.0.1:18080`. No reverse-proxy, Traefik, Cloudflare or firewall configuration is changed.
|
||||
|
||||
```sh
|
||||
bash cms security
|
||||
```
|
||||
|
||||
The command generates `CROWDSEC_LAPI_API_KEY`, writes the CrowdSec flags into `.env`, starts the engine and registers the `cms` bouncer. The anti-DDoS gate then consults the local LAPI per client IP (short-cached) and blocks `ban`/`captcha` decisions before its own rate buckets. `bash cms security status` reports engine state and `bash cms security disable` stops the engine and flips the toggle off.
|
||||
|
||||
The engine does not enroll into the CrowdSec Central API (`DISABLE_ONLINE_API=true`) and runs without the agent (`DISABLE_AGENT=true`), so it needs no account and no outbound access to `crowdsec.net` (often blocked on hardened hosts). Blocking comes from the imported blocklists plus the app's own rate buckets; it does not parse the host Nginx log. The app still has its separate opt-in traffic-sharing channel via `CROWDSEC_REPORT_ENABLED`. Change `CROWDSEC_LAPI_PORT` and `CROWDSEC_LAPI_URL` together when `18080` is already in use. `CROWDSEC_NGINX_LOG_DIR` is honored for when the agent is re-enabled.
|
||||
|
||||
This bouncer is application-layer: it sheds known-bad IPs at the CMS process and only for traffic that reaches the Next.js proxy. It does not drop traffic before the origin, does not protect other host ports/services, and depends on the client IP being trustworthy at the ingress. Keep the upstream protections (Cloudflare IP rules, proxy rate limits) for defense before the origin.
|
||||
|
||||
The running CMS loads the new env values on its next restart or deployment. For a CI-managed `epicnext-cms-app`, the next deploy (which sources `.env`) applies them; for a clone, `bash cms update --skip-pull` restarts it. `.env` now holds the LAPI key — keep its permissions restrictive.
|
||||
|
||||
External IP blocklists (Spamhaus, DShield, CINS, blocklist.de, abuse.ch, IPsum, Firehol, Tor exit nodes, …) can be synced into the local LAPI with `bash cms security blocklists`, and hourly with `bash cms security blocklists-install-cron` (no account, but internet to fetch). Configure via `CROWDSEC_BLOCKLIST_*`.
|
||||
|
||||
## Routine and selected-release updates
|
||||
|
||||
```sh
|
||||
|
||||
@@ -12,17 +12,25 @@ The command writes `report.json` and `report.md` and prints the Markdown report.
|
||||
|
||||
For each configured App Router route, resolve its exact app path using `app-path-routes-manifest.json` and `server/app-paths-manifest.json`. Read its generated `page_client-reference-manifest.js` as a JSON assignment **without executing JavaScript**. Use its sibling `page/build-manifest.json`, falling back to the root build manifest only if that sibling is absent.
|
||||
|
||||
The **initial entry envelope** is the union of route bootstrap `rootMainFilesTree[appPath]` (or `rootMainFiles`) and every `entryJSFiles` list in that route's client-reference manifest. This includes layout, page and boundary/loading entries. The definition follows the data exposed by the installed Next 16.3.4 Turbopack build and the `getLinkAndScriptTags` / `getRequiredScripts` renderer helpers; it is deliberately a build-artifact envelope, not a browser network trace. Conditional rendering, redirects, streaming and browser caches can change actual requests.
|
||||
The **initial entry envelope** is the union of route bootstrap `rootMainFilesTree[appPath]` (or `rootMainFiles`) and every client chunk that route's client-reference manifest lists. This includes layout, page and boundary/loading entries.
|
||||
|
||||
The manifest exposes those chunks differently per bundler. Turbopack emits an explicit per-segment `entryJSFiles` map; webpack emits no such field and records chunks only per client module, as `clientModules[*].chunks`, in `[chunkId, fileName, chunkId, fileName, …]` order. The report reads `entryJSFiles` when present and otherwise derives the same envelope from `clientModules`, which is the source Next's own `static-routes-info` uses. Numeric chunk ids are skipped; a malformed chunk *path* still fails rather than being dropped, so a broken manifest cannot quietly under-report a route.
|
||||
|
||||
> The build runs webpack (`next build --webpack`), so the `clientModules` path is the live one. An earlier revision only read `entryJSFiles`, and after the switch to webpack every route reported `unavailable` while the command still exited 0 — the budgets were silently not being measured. When a bundler switch changes the manifest layout again, re-check this section rather than trusting a clean exit.
|
||||
|
||||
The definition follows the data exposed by the installed Next 16.3.8 build and the `getLinkAndScriptTags` / `getRequiredScripts` renderer helpers; it is deliberately a build-artifact envelope, not a browser network trace. Conditional rendering, redirects, streaming and browser caches can change actual requests.
|
||||
|
||||
- Raw bytes are filesystem byte lengths of unique JavaScript assets in that envelope.
|
||||
- Gzip bytes are the **sum of independent gzip level 9 compressions** of those files using the recorded Node/zlib runtime. They are not gzip of concatenated source, nor observed CDN transfer sizes.
|
||||
- Deployment query strings and `/_next/` prefixes are normalized before deduplication. Shared files count once per route; each route is measured independently, with no misleading cross-route total.
|
||||
- Legacy `nomodule` polyfills are measured separately, outside the modern initial budget. CSS, source maps, images, external scripts, HTML/RSC payloads and async-only chunks absent from `entryJSFiles` are excluded.
|
||||
- Legacy `nomodule` polyfills are measured separately, outside the modern initial budget. CSS, source maps, images, external scripts, HTML/RSC payloads and async-only chunks absent from the manifest's chunk lists are excluded.
|
||||
- This report makes no claims about execution cost, LCP, hydration time or real-user performance.
|
||||
|
||||
## Initial limits
|
||||
|
||||
The first limits are **baseline bytes × 1.15, rounded upward to the next 10 KiB (10,240 bytes)** independently for raw and gzip. They are provisional size alerts, not validated speed targets. Baseline: existing local production build `build-TfctsWXpff2fKS`, Next 16.3.4; its source commit was not inferred.
|
||||
The first limits are **baseline bytes × 1.15, rounded upward to the next 10 KiB (10,240 bytes)** independently for raw and gzip. They are provisional size alerts, not validated speed targets. Baseline: local production build `build-TfctsWXpff2fKS`, Next 16.3.4 **Turbopack**; its source commit was not inferred.
|
||||
|
||||
The production build now runs webpack, so the numbers it reports are not directly comparable to the baseline below. Re-measured on the current webpack build the routes land at `/me` 786138/247738, `/news` 781601/245672, `/events` 782011/245923, `/search` 783262/246578, `/admin/catalog` 1172089/370843, `/admin/studio/furni` 1374128/440546 (raw/gzip). All remain inside the limits below, but `/admin/studio/furni` sits at ~98% of its gzip limit, so the next dependency added to that route will trip it. Recalibrate the table and `scripts/performance-budgets.json` together if the intent is to reset the baseline on webpack.
|
||||
|
||||
| Route | Baseline raw bytes | Baseline gzip bytes | Raw limit | Gzip limit |
|
||||
| --- | ---: | ---: | ---: | ---: |
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
CREATE TABLE `website_daily_reward_claims` (
|
||||
`id` bigint unsigned AUTO_INCREMENT NOT NULL,
|
||||
`user_id` int NOT NULL,
|
||||
`claim_date` date NOT NULL,
|
||||
`streak` int unsigned NOT NULL DEFAULT 1,
|
||||
`reward_day` int unsigned NOT NULL DEFAULT 1,
|
||||
`currency` varchar(20) NOT NULL DEFAULT 'credits',
|
||||
`amount` int NOT NULL DEFAULT 0,
|
||||
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
CONSTRAINT `website_daily_reward_claims_id` PRIMARY KEY(`id`),
|
||||
CONSTRAINT `daily_claim_user_date_uk` UNIQUE(`user_id`,`claim_date`)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE `website_daily_rewards` (
|
||||
`id` int unsigned AUTO_INCREMENT NOT NULL,
|
||||
`day` int unsigned NOT NULL DEFAULT 1,
|
||||
`currency` varchar(20) NOT NULL DEFAULT 'credits',
|
||||
`amount` int NOT NULL DEFAULT 0,
|
||||
`created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
CONSTRAINT `website_daily_rewards_id` PRIMARY KEY(`id`)
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE `admin_audit_log` MODIFY COLUMN `before` mediumtext;--> statement-breakpoint
|
||||
ALTER TABLE `admin_audit_log` MODIFY COLUMN `after` mediumtext;
|
||||
File diff suppressed because it is too large.
Load diff
@@ -8,6 +8,13 @@
|
||||
"when": 1788791201804,
|
||||
"tag": "0000_premium_spirit",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 1,
|
||||
"version": "5",
|
||||
"when": 1791828901011,
|
||||
"tag": "0001_breezy_maestro",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
-- Repair the escalation introduced by 0018's rule 1 ("has admin.dashboard gets
|
||||
-- ALL admin.*"). Migrating 0011 grants admin.dashboard to every rank >= 6 so
|
||||
-- that the sidebar opens, which meant rank 6 silently acquired
|
||||
-- admin.permissions.manage, admin.rcon.execute, admin.settings.edit,
|
||||
-- admin.users.edit, admin.users.reset_password, admin.room.delete, ...
|
||||
--
|
||||
-- Rule 1 is narrowed to `admin.%.view` (read-only, all the sidebar needs) in
|
||||
-- both the migration set and the runtime repair action. This migration undoes
|
||||
-- the over-grant on databases that already ran 0018: every role below the top
|
||||
-- rank keeps dashboard + *.view and loses every other admin.* grant. Ranks
|
||||
-- that legitimately hold tools keep them, because rule 3 only targets
|
||||
-- rank >= 7 and those roles are not touched here.
|
||||
--
|
||||
-- Note on the rank extraction: `acl_roles.slug` looks like `rank_7`, and
|
||||
-- MySQL's SUBSTRING is 1-based, so the digits start at position 6 — right
|
||||
-- after the 5-character `rank_`. Reading from position 7 truncates the first
|
||||
-- digit, which turns rank_10 into 0 and rank_7 into an empty string, i.e. both
|
||||
-- would compare as < 7 and lose grants this migration is supposed to preserve.
|
||||
-- The REGEXP guard below guarantees the remainder really is all digits.
|
||||
|
||||
DELETE `amp`
|
||||
FROM `acl_model_permissions` `amp`
|
||||
JOIN `acl_roles` `ar`
|
||||
ON `ar`.`id` = `amp`.`model_id`
|
||||
AND `amp`.`model_type` = 'Role'
|
||||
JOIN `acl_permissions` `ap`
|
||||
ON `ap`.`id` = `amp`.`permission_id`
|
||||
WHERE `ap`.`slug` LIKE 'admin.%'
|
||||
AND `ap`.`slug` NOT LIKE '%.view'
|
||||
AND `ar`.`slug` REGEXP '^rank_[0-9]+$'
|
||||
AND CAST(SUBSTRING(`ar`.`slug`, 6) AS UNSIGNED) < 7;
|
||||
@@ -0,0 +1,19 @@
|
||||
-- 0035_users_mail_index.sql
|
||||
-- Index on users.mail.
|
||||
--
|
||||
-- The authentication paths all look an account up by mail: password reset,
|
||||
-- e-mail verification, duplicate-address detection and the verify/resend
|
||||
-- cooldown all resolve a single user from a submitted address. Without an index
|
||||
-- each of those is a full table scan of `users`, which grows with every
|
||||
-- registration.
|
||||
--
|
||||
-- Deliberately NOT unique. Legacy rows predate the duplicate-address handling
|
||||
-- and can legitimately contain the same address more than once, so a unique
|
||||
-- index would fail to apply on an existing database. The lookup is made
|
||||
-- deterministic by ordering on `id` (see requestReset / the verify page), which
|
||||
-- is stable without the index and correct with it.
|
||||
--
|
||||
-- The column is VARCHAR(500), which exceeds the 767-byte InnoDB prefix limit on
|
||||
-- older row formats, hence an explicit 191-character prefix: enough to make the
|
||||
-- lookup selective and still indexable everywhere.
|
||||
CREATE INDEX IF NOT EXISTS `users_mail_index` ON `users` (`mail`(191));
|
||||
@@ -0,0 +1,48 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
import { expectNoApplicationError } from "./helpers";
|
||||
|
||||
// Unauthenticated requests to private routes must bounce to the login form —
|
||||
// never render private content and never 500. Two guard paths exist: the edge
|
||||
// proxy rewrites `/admin/*` to `/login?from=…` (`src/proxy.ts`), while the
|
||||
// member pages and the mod console redirect from their layouts with a bare
|
||||
// `/login`.
|
||||
|
||||
// Routes where the proxy preserves the requested destination.
|
||||
const PROXY_ROUTES = ["/admin", "/admin/settings"] as const;
|
||||
// Routes guarded by a page/layout `redirect("/login")` with no destination.
|
||||
const LAYOUT_ROUTES = [
|
||||
"/settings",
|
||||
"/me",
|
||||
"/messages",
|
||||
"/notifications",
|
||||
"/friends",
|
||||
"/redeem",
|
||||
"/mod",
|
||||
"/client",
|
||||
] as const;
|
||||
|
||||
for (const route of PROXY_ROUTES) {
|
||||
test(`${route} redirects to login and keeps the return path`, async ({
|
||||
page,
|
||||
}) => {
|
||||
const response = await page.goto(route);
|
||||
expect(response?.ok(), `GET ${route} -> ${response?.status()}`).toBe(true);
|
||||
await expect(page).toHaveURL(/\/login/);
|
||||
await expect(page.locator('input[type="password"]').first()).toBeVisible();
|
||||
await expectNoApplicationError(page);
|
||||
|
||||
const from = new URL(page.url()).searchParams.get("from");
|
||||
expect(from, "login lost the return destination").toBeTruthy();
|
||||
expect(new URL(from as string, "http://localhost").pathname).toBe(route);
|
||||
});
|
||||
}
|
||||
|
||||
for (const route of LAYOUT_ROUTES) {
|
||||
test(`${route} redirects an anonymous visitor to login`, async ({ page }) => {
|
||||
const response = await page.goto(route);
|
||||
expect(response?.ok(), `GET ${route} -> ${response?.status()}`).toBe(true);
|
||||
await expect(page).toHaveURL(/\/login/);
|
||||
await expect(page.locator('input[type="password"]').first()).toBeVisible();
|
||||
await expectNoApplicationError(page);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { expect, type Page } from "@playwright/test";
|
||||
|
||||
/**
|
||||
* Shared assertions for the read-only public e2e suite. These specs run against
|
||||
* a live CMS (see `playwright.config.ts`) and must never mutate data — they
|
||||
* guard the "everything loads, nothing is broken" contract that no unit test
|
||||
* can see.
|
||||
*/
|
||||
|
||||
/** Fails if Next rendered a runtime/server error boundary into the page. */
|
||||
export async function expectNoApplicationError(page: Page) {
|
||||
await expect(page.locator("body")).not.toContainText("Application error");
|
||||
await expect(page.locator("body")).not.toContainText("Internal Server Error");
|
||||
}
|
||||
|
||||
/**
|
||||
* Every <img> that finished loading must have decoded pixels. A failed request
|
||||
* (404, broken CMS media, bad avatar figure) reports `complete === true` with
|
||||
* `naturalWidth === 0`, which is what this catches. Intentionally lazy images
|
||||
* that are still pending (`complete === false`) are ignored.
|
||||
*/
|
||||
export async function expectNoBrokenImages(page: Page) {
|
||||
const broken = await page.evaluate(() => {
|
||||
return Array.from(document.images)
|
||||
.filter((img) => {
|
||||
const src = img.currentSrc || img.src;
|
||||
if (!src || src.startsWith("data:")) return false;
|
||||
return img.complete && img.naturalWidth === 0;
|
||||
})
|
||||
.map((img) => img.currentSrc || img.src);
|
||||
});
|
||||
expect(broken, `broken images:\n${broken.join("\n")}`).toEqual([]);
|
||||
}
|
||||
|
||||
/** No horizontal scrollbar — the classic mobile layout regression. */
|
||||
export async function expectNoHorizontalOverflow(page: Page) {
|
||||
const overflowing = await page.evaluate(() => {
|
||||
const doc = document.documentElement;
|
||||
return doc.scrollWidth > window.innerWidth + 1;
|
||||
});
|
||||
expect(overflowing, "page scrolls horizontally").toBe(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks the page top-to-bottom so `loading="lazy"` images below the fold get a
|
||||
* chance to start before the broken-image check runs.
|
||||
*/
|
||||
export async function scrollThroughPage(page: Page) {
|
||||
await page.evaluate(async () => {
|
||||
const step = window.innerHeight;
|
||||
for (let y = 0; y < document.body.scrollHeight; y += step) {
|
||||
window.scrollTo(0, y);
|
||||
await new Promise((resolve) => setTimeout(resolve, 80));
|
||||
}
|
||||
window.scrollTo(0, 0);
|
||||
});
|
||||
}
|
||||
|
||||
/** Skips a spec when the CMS answered with a maintenance redirect. */
|
||||
export function isMaintenanceRedirect(url: string) {
|
||||
return /\/maintenance(?:$|[/?])/.test(url);
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
import {
|
||||
avatarPath,
|
||||
countBytes,
|
||||
fetchThroughEdge,
|
||||
getThroughEdge,
|
||||
looksLikeImage,
|
||||
} from "./imaging-target";
|
||||
|
||||
// Endpoints exercised here are identical whether the CMS is reached directly
|
||||
// or through the edge proxy, so this spec runs unchanged in the deploy gate and
|
||||
// in the cross-browser job. Topology-specific checks live in
|
||||
// `cms-imaging.spec.ts` (direct only) and `public-routing.spec.ts` (edge only).
|
||||
|
||||
const IMAGE_SOURCES = new Set([
|
||||
"primary",
|
||||
"fallback",
|
||||
"cache",
|
||||
"cache-stale",
|
||||
"unavailable",
|
||||
]);
|
||||
|
||||
test("renders an animated APNG for a figure with an effect", async ({
|
||||
request,
|
||||
}) => {
|
||||
const response = await getThroughEdge(
|
||||
request,
|
||||
avatarPath({ img_format: "apng", effect: "14" }),
|
||||
);
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/");
|
||||
// `unavailable` means the placeholder was served, i.e. no real render.
|
||||
expect(response.headers()["x-imager-source"]).not.toBe("unavailable");
|
||||
|
||||
const body = await response.body();
|
||||
expect(body.length).toBeGreaterThan(1000);
|
||||
// APNG signature chunks: `acTL` announces the animation, `fcTL` marks frames.
|
||||
expect(body.includes(Buffer.from("acTL"))).toBe(true);
|
||||
expect(countBytes(body, Buffer.from("fcTL"))).toBeGreaterThan(1);
|
||||
});
|
||||
|
||||
test("serves a static PNG when no animation is requested", async ({
|
||||
request,
|
||||
}) => {
|
||||
const response = await getThroughEdge(
|
||||
request,
|
||||
avatarPath({ img_format: "png" }),
|
||||
);
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/png");
|
||||
const body = await response.body();
|
||||
expect(body.subarray(1, 4).toString("ascii")).toBe("PNG");
|
||||
});
|
||||
|
||||
test("advertises a cacheable response and a known render source", async ({
|
||||
request,
|
||||
}) => {
|
||||
const response = await getThroughEdge(
|
||||
request,
|
||||
avatarPath({ img_format: "png" }),
|
||||
);
|
||||
expect(response.status()).toBe(200);
|
||||
|
||||
const source = response.headers()["x-imager-source"];
|
||||
expect(source, "x-imager-source header").toBeTruthy();
|
||||
expect(IMAGE_SOURCES.has(source as string)).toBe(true);
|
||||
|
||||
const cacheControl = response.headers()["cache-control"];
|
||||
expect(cacheControl, "cache-control header").toContain("public");
|
||||
expect(cacheControl).toMatch(/max-age=\d+/);
|
||||
|
||||
// Render sources other than the graceful placeholder are worth caching.
|
||||
if (source !== "unavailable") {
|
||||
expect(cacheControl).not.toContain("no-cache");
|
||||
}
|
||||
});
|
||||
|
||||
test("answers conditional requests with 304", async ({ request }) => {
|
||||
const first = await getThroughEdge(
|
||||
request,
|
||||
avatarPath({ img_format: "png" }),
|
||||
);
|
||||
expect(first.status()).toBe(200);
|
||||
const etag = first.headers()["etag"];
|
||||
expect(etag).toBeTruthy();
|
||||
|
||||
const second = await getThroughEdge(
|
||||
request,
|
||||
avatarPath({ img_format: "png" }),
|
||||
{
|
||||
headers: { "if-none-match": etag as string },
|
||||
},
|
||||
);
|
||||
expect(second.status()).toBe(304);
|
||||
expect(second.headers()["etag"]).toBe(etag);
|
||||
});
|
||||
|
||||
test("serves the rendered avatar behind permissive CORS", async ({
|
||||
request,
|
||||
}) => {
|
||||
const response = await getThroughEdge(
|
||||
request,
|
||||
avatarPath({ img_format: "png" }),
|
||||
);
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["access-control-allow-origin"]).toBe("*");
|
||||
});
|
||||
|
||||
test("preflight requests advertise GET and revalidation", async ({
|
||||
request,
|
||||
}) => {
|
||||
const response = await fetchThroughEdge(request, avatarPath(), {
|
||||
method: "OPTIONS",
|
||||
});
|
||||
expect(response.status()).toBe(204);
|
||||
expect(response.headers()["access-control-allow-origin"]).toBe("*");
|
||||
expect(response.headers()["access-control-allow-methods"]).toContain("GET");
|
||||
expect(response.headers()["access-control-allow-headers"]).toContain(
|
||||
"If-None-Match",
|
||||
);
|
||||
});
|
||||
|
||||
test.describe("rejects malformed figures", () => {
|
||||
const cases: Array<{ name: string; query: string }> = [
|
||||
{ name: "missing figure", query: "" },
|
||||
{ name: "empty figure", query: "?figure=" },
|
||||
{ name: "path traversal", query: "?figure=../bad" },
|
||||
{ name: "separator injection", query: "?figure=hd-180-1/../x" },
|
||||
{
|
||||
name: "over-long figure",
|
||||
query: `?figure=hd-180-${"x".repeat(600)}`,
|
||||
},
|
||||
{
|
||||
name: "too many parts",
|
||||
query: `?figure=hd-180${".a-1".repeat(30)}`,
|
||||
},
|
||||
];
|
||||
|
||||
for (const { name, query } of cases) {
|
||||
test(name, async ({ request }) => {
|
||||
const response = await getThroughEdge(
|
||||
request,
|
||||
`/api/imaging/avatar${query}`,
|
||||
);
|
||||
expect(response.status()).toBe(400);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test("collapses a burst of concurrent requests for one figure", async ({
|
||||
request,
|
||||
}) => {
|
||||
// Vary the key so this can hit a cold (or at least distinct) render while a
|
||||
// page-load style burst is in flight. Every response must still be a real
|
||||
// image — single-flight must not turn contention into 5xx/placeholders.
|
||||
const path = avatarPath({
|
||||
img_format: "png",
|
||||
direction: "6",
|
||||
head_direction: "1",
|
||||
});
|
||||
const responses = await Promise.all(
|
||||
Array.from({ length: 6 }, () => getThroughEdge(request, path)),
|
||||
);
|
||||
for (const response of responses) {
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/");
|
||||
expect(looksLikeImage(await response.body())).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
test("the badge endpoint serves an image and revalidates", async ({
|
||||
request,
|
||||
}) => {
|
||||
const first = await getThroughEdge(request, "/api/imaging/badge?code=ADM");
|
||||
expect(first.status()).toBe(200);
|
||||
expect(first.headers()["content-type"]).toContain("image/");
|
||||
expect(first.headers()["access-control-allow-origin"]).toBe("*");
|
||||
const etag = first.headers()["etag"];
|
||||
expect(etag).toBeTruthy();
|
||||
|
||||
const second = await getThroughEdge(request, "/api/imaging/badge?code=ADM", {
|
||||
headers: { "if-none-match": etag as string },
|
||||
});
|
||||
expect(second.status()).toBe(304);
|
||||
});
|
||||
|
||||
test("the badge endpoint can resolve from a local source", async ({
|
||||
request,
|
||||
}) => {
|
||||
const response = await getThroughEdge(
|
||||
request,
|
||||
"/api/imaging/badge?code=ADM&source=local",
|
||||
);
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/");
|
||||
expect(response.headers()["x-imager-source"]).toBeTruthy();
|
||||
});
|
||||
|
||||
test.describe("rejects malformed badge codes", () => {
|
||||
const cases = [
|
||||
{ name: "missing code", query: "" },
|
||||
{ name: "empty code", query: "?code=" },
|
||||
{ name: "path traversal", query: "?code=../x" },
|
||||
{ name: "too long", query: `?code=${"A".repeat(51)}` },
|
||||
];
|
||||
|
||||
for (const { name, query } of cases) {
|
||||
test(name, async ({ request }) => {
|
||||
const response = await getThroughEdge(
|
||||
request,
|
||||
`/api/imaging/badge${query}`,
|
||||
);
|
||||
expect(response.status()).toBe(400);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test("the badge preflight advertises GET", async ({ request }) => {
|
||||
const response = await fetchThroughEdge(
|
||||
request,
|
||||
"/api/imaging/badge?code=ADM",
|
||||
{
|
||||
method: "OPTIONS",
|
||||
},
|
||||
);
|
||||
expect(response.status()).toBe(204);
|
||||
expect(response.headers()["access-control-allow-methods"]).toContain("GET");
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
import { avatarPath } from "./imaging-target";
|
||||
|
||||
/**
|
||||
* Cross-browser check that a real browser (chromium/firefox/webkit) can decode
|
||||
* the avatar the imager produces and that it actually animates. The endpoint
|
||||
* spec covers the HTTP contract; this one covers the client-side decode, where
|
||||
* an unsupported image format would surface as a blank/broken avatar.
|
||||
*
|
||||
* Navigates straight to the image URL (a top-level navigation, which CSP
|
||||
* `upgrade-insecure-requests` does not rewrite) rather than embedding it in a
|
||||
* page: over a plain-HTTP host the upgrade directive would otherwise send the
|
||||
* subresource request to https and break the load, without exercising anything
|
||||
* about the image itself.
|
||||
*
|
||||
* Run across engines via `pnpm test:imaging` (playwright.imaging.config.ts).
|
||||
*/
|
||||
const ANIMATED = avatarPath({ img_format: "apng", effect: "14" });
|
||||
const STATIC = avatarPath({ img_format: "png" });
|
||||
|
||||
test("the avatar decodes and animates in the browser", async ({ page }) => {
|
||||
const response = await page.goto(ANIMATED);
|
||||
expect(response?.status()).toBe(200);
|
||||
|
||||
const image = page.locator("img");
|
||||
await expect(image).toBeVisible();
|
||||
const decoded = await image.evaluate((element: HTMLImageElement) => ({
|
||||
width: element.naturalWidth,
|
||||
height: element.naturalHeight,
|
||||
}));
|
||||
expect(decoded.width).toBeGreaterThan(0);
|
||||
expect(decoded.height).toBeGreaterThan(0);
|
||||
|
||||
// An animated APNG keeps producing new frames; a static/undecodable one
|
||||
// yields the same pixels every time. Sampling screenshots over a second is
|
||||
// enough to tell them apart without depending on exact frame timing.
|
||||
const frames = new Set<string>();
|
||||
for (let attempt = 0; attempt < 8; attempt += 1) {
|
||||
frames.add((await image.screenshot()).toString("base64"));
|
||||
await page.waitForTimeout(180);
|
||||
}
|
||||
expect(frames.size).toBeGreaterThan(1);
|
||||
});
|
||||
|
||||
test("the static render matches its committed visual baseline", async ({
|
||||
page,
|
||||
}) => {
|
||||
// Pins the exact pixels of a non-animated render so a renderer/asset drift
|
||||
// (wrong palette, missing part, changed sizing) is caught even when the
|
||||
// image still decodes. Baselines are per engine and committed under
|
||||
// e2e/imaging/__screenshots__/.
|
||||
const response = await page.goto(STATIC);
|
||||
expect(response?.status()).toBe(200);
|
||||
await expect(page.locator("img")).toHaveScreenshot("static-avatar.png", {
|
||||
animations: "disabled",
|
||||
});
|
||||
});
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 3.1 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 3.6 KiB |
@@ -0,0 +1,78 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
import { countBytes, FIGURE, IS_EDGE, looksLikeImage } from "./imaging-target";
|
||||
|
||||
// These assertions only hold when the CMS itself answers `/imaging`. In
|
||||
// production the edge proxy fronts bare `/imaging` (and `/imaging/*`) to the
|
||||
// imager container, so the CMS route is shadowed; set `IMAGING_EDGE=1` to skip.
|
||||
test.skip(
|
||||
IS_EDGE,
|
||||
"The CMS /imaging route is shadowed by the edge proxy in production",
|
||||
);
|
||||
|
||||
const imagerPath = (params: Record<string, string> = {}) =>
|
||||
`/imaging?${new URLSearchParams({ figure: FIGURE, img_format: "png", ...params })}`;
|
||||
|
||||
const PNG_MAGIC = Buffer.from("89504e470d0a1a0a", "hex");
|
||||
|
||||
test("serves the default render as a PNG", async ({ request }) => {
|
||||
const response = await request.get(imagerPath());
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/png");
|
||||
const body = await response.body();
|
||||
expect(body.subarray(0, 8).equals(PNG_MAGIC)).toBe(true);
|
||||
expect(looksLikeImage(body)).toBe(true);
|
||||
});
|
||||
|
||||
test("transcodes to WebP on request", async ({ request }) => {
|
||||
const response = await request.get(imagerPath({ format: "webp" }));
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/webp");
|
||||
const body = await response.body();
|
||||
expect(body.subarray(0, 4).toString("ascii")).toBe("RIFF");
|
||||
expect(body.subarray(8, 12).toString("ascii")).toBe("WEBP");
|
||||
});
|
||||
|
||||
test("transcodes to AVIF on request", async ({ request }) => {
|
||||
const response = await request.get(imagerPath({ format: "avif" }));
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/avif");
|
||||
const body = await response.body();
|
||||
expect(body.subarray(4, 12).toString("ascii")).toBe("ftypavif");
|
||||
});
|
||||
|
||||
test("leaves animated APNG renders untranscoded", async ({ request }) => {
|
||||
// The renderer emits effected avatars as APNG; libvips here reads only the
|
||||
// first frame, so the animated original must be served as-is even when a
|
||||
// transcode is requested — otherwise the avatar would stop animating.
|
||||
const response = await request.get(
|
||||
imagerPath({ img_format: "apng", effect: "14", format: "webp" }),
|
||||
);
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/png");
|
||||
|
||||
const body = await response.body();
|
||||
expect(body.includes(Buffer.from("acTL"))).toBe(true);
|
||||
expect(countBytes(body, Buffer.from("fcTL"))).toBeGreaterThan(1);
|
||||
});
|
||||
|
||||
test("advertises long-lived edge caching", async ({ request }) => {
|
||||
const response = await request.get(imagerPath());
|
||||
expect(response.status()).toBe(200);
|
||||
const cacheControl = response.headers()["cache-control"] ?? "";
|
||||
expect(cacheControl).toContain("public");
|
||||
expect(cacheControl).toContain("s-maxage");
|
||||
expect(cacheControl).toContain("stale-while-revalidate");
|
||||
});
|
||||
|
||||
test("rejects a malformed figure with 400", async ({ request }) => {
|
||||
const response = await request.get(
|
||||
`/imaging?figure=${encodeURIComponent("../bad")}`,
|
||||
);
|
||||
expect(response.status()).toBe(400);
|
||||
});
|
||||
|
||||
test("the imager preflight advertises GET", async ({ request }) => {
|
||||
const response = await request.fetch("/imaging", { method: "OPTIONS" });
|
||||
expect(response.status()).toBe(204);
|
||||
expect(response.headers()["access-control-allow-methods"]).toContain("GET");
|
||||
});
|
||||
@@ -0,0 +1,103 @@
|
||||
import type { APIRequestContext, APIResponse } from "@playwright/test";
|
||||
|
||||
/**
|
||||
* Where the imaging specs are pointed.
|
||||
*
|
||||
* - Deploy gate (`playwright.config.ts`): hits the CMS directly, so the CMS's
|
||||
* own `/imaging` route answers and its transcoding variants are reachable.
|
||||
* - Cross-browser job (`playwright.imaging.config.ts` against the public
|
||||
* origin): the edge proxy fronts bare `/imaging` (and `/imaging/*`) to the
|
||||
* imager container, so the CMS route is shadowed. Only the endpoints that are
|
||||
* identical in both topologies may be asserted unconditionally.
|
||||
*
|
||||
* Set `IMAGING_EDGE=1` when the base URL is behind the edge proxy.
|
||||
*/
|
||||
export const IS_EDGE = process.env.IMAGING_EDGE === "1";
|
||||
|
||||
/**
|
||||
* Failures that mean "the request never reached the app".
|
||||
*
|
||||
* Cloudflare answers the 520-527 family when its own handshake to the origin
|
||||
* fails, and gateways add 502/503/504. Retrying those is not papering over a
|
||||
* bug: the assertion under test is about the CMS, and a 525 says nothing about
|
||||
* it. A genuinely broken edge still exhausts the retries and fails the test.
|
||||
*/
|
||||
const TRANSIENT_EDGE_STATUSES = new Set([
|
||||
502, 503, 504, 520, 521, 522, 523, 524, 525, 526, 527,
|
||||
]);
|
||||
const EDGE_RETRY_ATTEMPTS = 3;
|
||||
const EDGE_RETRY_DELAY_MS = 500;
|
||||
|
||||
async function withEdgeRetry(
|
||||
send: () => Promise<APIResponse>,
|
||||
): Promise<APIResponse> {
|
||||
let response = await send();
|
||||
for (
|
||||
let attempt = 1;
|
||||
attempt < EDGE_RETRY_ATTEMPTS &&
|
||||
TRANSIENT_EDGE_STATUSES.has(response.status());
|
||||
attempt += 1
|
||||
) {
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(resolve, EDGE_RETRY_DELAY_MS * attempt),
|
||||
);
|
||||
response = await send();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
/** GET that survives a transient edge/transport failure. */
|
||||
export function getThroughEdge(
|
||||
request: APIRequestContext,
|
||||
url: string,
|
||||
options: Parameters<APIRequestContext["get"]>[1] = {},
|
||||
): Promise<APIResponse> {
|
||||
return withEdgeRetry(() => request.get(url, options));
|
||||
}
|
||||
|
||||
/** POST/OPTIONS/… counterpart of `getThroughEdge`. */
|
||||
export function fetchThroughEdge(
|
||||
request: APIRequestContext,
|
||||
url: string,
|
||||
options: Parameters<APIRequestContext["fetch"]>[1] = {},
|
||||
): Promise<APIResponse> {
|
||||
return withEdgeRetry(() => request.fetch(url, options));
|
||||
}
|
||||
|
||||
// The exact figure that regressed in production: the game client appends a
|
||||
// trailing `.<n>` segment that the renderer ignores, but the previous
|
||||
// validation regex rejected with a 400.
|
||||
export const FIGURE =
|
||||
"hr-11782-40-40.hd-180-7-14.ch-11592-66.lg-10726-79-1408.sh-11764-1408.ha-11958-70-1408.wa-2007-0.0";
|
||||
|
||||
export const avatarPath = (params: Record<string, string> = {}) =>
|
||||
`/api/imaging/avatar?${new URLSearchParams({ figure: FIGURE, ...params })}`;
|
||||
|
||||
/** Public imager endpoint served through the edge proxy (imager container). */
|
||||
export const IMAGER_PATH = (params: Record<string, string> = {}) =>
|
||||
`/imaging/avatarimage?${new URLSearchParams({ figure: FIGURE, ...params })}`;
|
||||
|
||||
/** Counts non-overlapping occurrences of `needle` inside `haystack`. */
|
||||
export function countBytes(haystack: Buffer, needle: Buffer): number {
|
||||
let count = 0;
|
||||
for (
|
||||
let index = haystack.indexOf(needle);
|
||||
index !== -1;
|
||||
index = haystack.indexOf(needle, index + needle.length)
|
||||
) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/** PNG/JPEG/GIF/WebP/AVIF signatures, so a test can prove a body is an image. */
|
||||
export function looksLikeImage(body: Buffer): boolean {
|
||||
if (body.length < 12) return false;
|
||||
if (body.subarray(0, 8).equals(Buffer.from("89504e470d0a1a0a", "hex")))
|
||||
return true; // PNG / APNG
|
||||
if (body.subarray(0, 3).toString("ascii") === "GIF") return true;
|
||||
if (body.subarray(0, 2).toString("hex") === "ffd8") return true; // JPEG
|
||||
if (body.subarray(0, 4).toString("ascii") === "RIFF") return true; // WebP
|
||||
if (body.subarray(4, 12).toString("ascii") === "ftypavif") return true; // AVIF
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
import { avatarPath } from "./imaging-target";
|
||||
|
||||
// Lightweight size/latency budget for the avatar path. The endpoint spec checks
|
||||
// correctness; this one catches a regression that keeps the image correct but
|
||||
// makes it oversized or slow — e.g. losing the disk/edge cache and re-encoding
|
||||
// (or re-rendering) on every hit. Budgets are deliberately loose: they should
|
||||
// flag a real regression, not a cold cache or a slow CI network.
|
||||
const STATIC_MAX_BYTES = 64 * 1024;
|
||||
const ANIMATED_MAX_BYTES = 512 * 1024;
|
||||
const WARM_MAX_MS = 3000;
|
||||
|
||||
test("the static avatar stays inside its byte budget", async ({ request }) => {
|
||||
const response = await request.get(avatarPath({ img_format: "png" }));
|
||||
expect(response.status()).toBe(200);
|
||||
expect((await response.body()).length).toBeLessThanOrEqual(STATIC_MAX_BYTES);
|
||||
});
|
||||
|
||||
test("the animated avatar stays inside its byte budget", async ({
|
||||
request,
|
||||
}) => {
|
||||
const response = await request.get(
|
||||
avatarPath({ img_format: "apng", effect: "14" }),
|
||||
);
|
||||
expect(response.status()).toBe(200);
|
||||
expect((await response.body()).length).toBeLessThanOrEqual(
|
||||
ANIMATED_MAX_BYTES,
|
||||
);
|
||||
});
|
||||
|
||||
test("a warm avatar request is served quickly", async ({ request }) => {
|
||||
const path = avatarPath({ img_format: "png" });
|
||||
await request.get(path); // warm the disk and edge caches
|
||||
const started = Date.now();
|
||||
const response = await request.get(path);
|
||||
const elapsed = Date.now() - started;
|
||||
expect([200, 304]).toContain(response.status());
|
||||
expect(elapsed).toBeLessThan(WARM_MAX_MS);
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
import {
|
||||
avatarPath,
|
||||
getThroughEdge,
|
||||
IMAGER_PATH,
|
||||
IS_EDGE,
|
||||
looksLikeImage,
|
||||
} from "./imaging-target";
|
||||
|
||||
// Regression guard for the public topology: the edge proxy must front the
|
||||
// imager container at `/imaging/*` while `/api/imaging/*` reaches the CMS.
|
||||
// Mis-routing here blanks avatars for every visitor, and no unit test sees it.
|
||||
// Only meaningful against the public origin, so it is skipped when the CMS is
|
||||
// reached directly (set `IMAGING_EDGE=1` for the public origin).
|
||||
test.skip(
|
||||
!IS_EDGE,
|
||||
"Public routing is only asserted against the edge-fronted origin",
|
||||
);
|
||||
|
||||
test("the edge serves the imager endpoint as an image", async ({ request }) => {
|
||||
const response = await getThroughEdge(
|
||||
request,
|
||||
IMAGER_PATH({ img_format: "png" }),
|
||||
);
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/");
|
||||
expect(looksLikeImage(await response.body())).toBe(true);
|
||||
});
|
||||
|
||||
test("the CMS avatar endpoint is reachable through the edge", async ({
|
||||
request,
|
||||
}) => {
|
||||
const response = await getThroughEdge(
|
||||
request,
|
||||
avatarPath({ img_format: "png" }),
|
||||
);
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/");
|
||||
expect(looksLikeImage(await response.body())).toBe(true);
|
||||
});
|
||||
|
||||
test("the edge does not shadow the CMS badge endpoint", async ({ request }) => {
|
||||
const response = await getThroughEdge(request, "/api/imaging/badge?code=ADM");
|
||||
expect(response.status()).toBe(200);
|
||||
expect(response.headers()["content-type"]).toContain("image/");
|
||||
});
|
||||
@@ -88,7 +88,10 @@ test("staff signs in, saves a draft, previews it and publishes to anonymous read
|
||||
await page
|
||||
.locator('input[autocomplete="current-password"]')
|
||||
.press("Enter");
|
||||
await expect(page).toHaveURL(/\/me(?:\?|$)/);
|
||||
// The login page honours `?from=`, so an admin bounced off /admin lands
|
||||
// back where they were heading instead of on /me. The step below
|
||||
// navigates there explicitly anyway; this asserts the redirect target.
|
||||
await expect(page).toHaveURL(/\/admin\/articles\/new(?:\?|$)/);
|
||||
const session = await context.request
|
||||
.get("/api/auth/session")
|
||||
.then((response) => response.json());
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
|
||||
// Caching contract for the anonymous pages. This is deterministic and stays in
|
||||
// the blocking deploy gate: the perf work relies on the edge/shared cache
|
||||
// (`src/proxy.ts`) — public content pages advertise a short `s-maxage`, while
|
||||
// session-dependent pages (login, register, settings) must stay
|
||||
// `private, no-store`. Timing budgets are advisory and live in
|
||||
// `e2e/quality/performance.spec.ts`.
|
||||
test("anonymous content pages advertise a shared cache", async ({
|
||||
request,
|
||||
}) => {
|
||||
for (const route of ["/news", "/staff", "/shop"]) {
|
||||
const response = await request.get(route);
|
||||
const cacheControl = response.headers()["cache-control"] ?? "";
|
||||
expect(cacheControl, `GET ${route} cache-control`).toContain("s-maxage");
|
||||
}
|
||||
});
|
||||
|
||||
test("session-dependent pages stay private and uncached", async ({
|
||||
request,
|
||||
}) => {
|
||||
for (const route of ["/login", "/register"]) {
|
||||
const response = await request.get(route);
|
||||
const cacheControl = response.headers()["cache-control"] ?? "";
|
||||
expect(cacheControl, `GET ${route} cache-control`).toContain("no-store");
|
||||
expect(cacheControl, `GET ${route} must not be shared`).not.toContain(
|
||||
"s-maxage",
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
import {
|
||||
expectNoApplicationError,
|
||||
expectNoBrokenImages,
|
||||
expectNoHorizontalOverflow,
|
||||
isMaintenanceRedirect,
|
||||
scrollThroughPage,
|
||||
} from "../helpers";
|
||||
|
||||
// ADVISORY suite — run with `pnpm test:e2e:quality`, not part of the deploy
|
||||
// gate. These checks depend on live content/upload state and user data (a
|
||||
// missing upload, a legacy malformed avatar figure, a bad theme asset), so
|
||||
// they are informational: they tell you the site has a real content problem,
|
||||
// but they must never block a code deploy. Every assertion the CMS can
|
||||
// guarantee on its own lives in the blocking specs instead.
|
||||
const PUBLIC_ROUTES = [
|
||||
"/",
|
||||
"/news",
|
||||
"/staff",
|
||||
"/shop",
|
||||
"/search",
|
||||
"/leaderboard",
|
||||
"/help",
|
||||
"/radio",
|
||||
] as const;
|
||||
|
||||
for (const route of PUBLIC_ROUTES) {
|
||||
test(`${route} loads every image and does not overflow`, async ({ page }) => {
|
||||
const response = await page.goto(route);
|
||||
expect(response?.ok(), `GET ${route} -> ${response?.status()}`).toBe(true);
|
||||
|
||||
await expect(page.locator("main").first()).toBeVisible();
|
||||
await expectNoApplicationError(page);
|
||||
|
||||
await scrollThroughPage(page);
|
||||
await expectNoBrokenImages(page);
|
||||
await expectNoHorizontalOverflow(page);
|
||||
});
|
||||
}
|
||||
|
||||
test("the register page loads its theme/avatar images", async ({ page }) => {
|
||||
const response = await page.goto("/register");
|
||||
test.skip(
|
||||
isMaintenanceRedirect(page.url()) || response?.status() === 404,
|
||||
"register is not reachable on this deploy",
|
||||
);
|
||||
await scrollThroughPage(page);
|
||||
await expectNoBrokenImages(page);
|
||||
await expectNoHorizontalOverflow(page);
|
||||
});
|
||||
|
||||
test("the homepage stays usable on a narrow viewport", async ({ page }) => {
|
||||
await page.setViewportSize({ width: 390, height: 844 });
|
||||
const response = await page.goto("/");
|
||||
expect(response?.ok()).toBe(true);
|
||||
await expectNoApplicationError(page);
|
||||
await expectNoHorizontalOverflow(page);
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
|
||||
// ADVISORY suite — run with `pnpm test:e2e:quality`. Warm-page latency depends
|
||||
// on the host, the edge and the shared data caches, so a slow CI hop can trip
|
||||
// these budgets without anything being wrong. The cache-control *contract*
|
||||
// (deterministic) lives in the blocking `e2e/performance.spec.ts`; only the
|
||||
// timing budgets are advisory.
|
||||
const BUDGET_MS = Number(process.env.E2E_LOAD_BUDGET_MS || 4000);
|
||||
|
||||
const WARM_ROUTES = ["/", "/news", "/staff", "/shop", "/login", "/register"];
|
||||
|
||||
for (const route of WARM_ROUTES) {
|
||||
test(`${route} responds within the warm budget`, async ({ request }) => {
|
||||
await request.get(route); // warm the render + data caches
|
||||
const started = Date.now();
|
||||
const response = await request.get(route);
|
||||
const elapsed = Date.now() - started;
|
||||
expect([200, 304]).toContain(response.status());
|
||||
expect(
|
||||
elapsed,
|
||||
`GET ${route} took ${elapsed}ms (budget ${BUDGET_MS}ms)`,
|
||||
).toBeLessThan(BUDGET_MS);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
import { expectNoApplicationError, isMaintenanceRedirect } from "./helpers";
|
||||
|
||||
// The register form is the gate for every new player, so it gets a deeper
|
||||
// read-only pass than the generic public-page spec. It also pins the captcha
|
||||
// contract that is configured through `CAPTCHA_PROVIDER` / `TURNSTILE_*` env
|
||||
// vars (see `src/lib/services/captcha.ts`): whatever provider the running CMS
|
||||
// resolves, the DOM must expose exactly one widget whose sitekey is non-empty.
|
||||
const CAPTCHA_SELECTOR = ".cf-turnstile, .g-recaptcha, .h-captcha";
|
||||
|
||||
// Optional explicit expectation for a deploy, e.g. `E2E_EXPECT_CAPTCHA=turnstile`
|
||||
// or `E2E_EXPECT_CAPTCHA=none`. Left unset, the spec only checks consistency.
|
||||
const EXPECT = process.env.E2E_EXPECT_CAPTCHA?.trim();
|
||||
|
||||
test.describe("register", () => {
|
||||
test.beforeEach(async ({ page }) => {
|
||||
const response = await page.goto("/register");
|
||||
test.skip(
|
||||
isMaintenanceRedirect(page.url()) || response?.status() === 404,
|
||||
"register is not reachable on this deploy",
|
||||
);
|
||||
});
|
||||
|
||||
test("renders every field a new player has to fill in", async ({ page }) => {
|
||||
await expect(page.locator('input[name="username"]')).toBeVisible();
|
||||
await expect(page.locator('input[name="mail"]')).toBeVisible();
|
||||
await expect(page.locator('input[name="password"]').first()).toBeVisible();
|
||||
await expect(
|
||||
page.locator('input[name="password_confirmation"]'),
|
||||
).toBeVisible();
|
||||
await expect(page.locator('input[name="terms"]')).toBeVisible();
|
||||
await expect(page.locator('button[type="submit"]').first()).toBeVisible();
|
||||
await expectNoApplicationError(page);
|
||||
});
|
||||
|
||||
test("exposes exactly one captcha widget with a site key", async ({
|
||||
page,
|
||||
}) => {
|
||||
const widgets = page.locator(CAPTCHA_SELECTOR);
|
||||
|
||||
if (EXPECT === "none") {
|
||||
await expect(widgets).toHaveCount(0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (EXPECT === "turnstile") {
|
||||
await expect(page.locator(".cf-turnstile")).toHaveCount(1);
|
||||
}
|
||||
|
||||
const count = await widgets.count();
|
||||
expect(
|
||||
count,
|
||||
`expected at most one captcha provider, found ${count}`,
|
||||
).toBeLessThanOrEqual(1);
|
||||
|
||||
for (let index = 0; index < count; index += 1) {
|
||||
const siteKey = await widgets.nth(index).getAttribute("data-sitekey");
|
||||
expect(siteKey, "captcha widget without data-sitekey").toBeTruthy();
|
||||
expect(siteKey?.trim().length).toBeGreaterThan(0);
|
||||
}
|
||||
|
||||
if (count > 0 && (await page.locator(".cf-turnstile").count()) > 0) {
|
||||
const script = page.locator(
|
||||
'script[src*="challenges.cloudflare.com/turnstile"]',
|
||||
);
|
||||
await expect(script).toHaveCount(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,7 @@ import { expect, test } from "@playwright/test";
|
||||
|
||||
const attachmentId = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
||||
const file = {
|
||||
name: "fixture_chair.nitro",
|
||||
name: "fixture_chair.hab",
|
||||
mimeType: "application/octet-stream",
|
||||
buffer: Buffer.from("isolated upload fixture"),
|
||||
};
|
||||
@@ -23,7 +23,7 @@ test("attachment upload and double-click resume send one retry and refresh histo
|
||||
expect(route.request().postData()).toContain('name="classname"');
|
||||
expect(route.request().postData()).toContain("fixture_chair");
|
||||
expect(route.request().postData()).toContain(
|
||||
'filename="fixture_chair.nitro"',
|
||||
'filename="fixture_chair.hab"',
|
||||
);
|
||||
await route.fulfill({ json: { ok: true, attachmentId } });
|
||||
});
|
||||
@@ -38,7 +38,7 @@ test("attachment upload and double-click resume send one retry and refresh histo
|
||||
await new Promise((resolve) => setTimeout(resolve, 150));
|
||||
await route.fulfill({ json: { ok: true } });
|
||||
});
|
||||
await page.getByLabel("Choose the original .nitro file").setInputFiles(file);
|
||||
await page.getByLabel("Choose the original .hab file").setInputFiles(file);
|
||||
await expect(
|
||||
page.getByText("Matching original file attached", { exact: true }),
|
||||
).toBeVisible();
|
||||
@@ -94,9 +94,7 @@ for (const scenario of [
|
||||
})
|
||||
: route.abort("failed"),
|
||||
);
|
||||
await page
|
||||
.getByLabel("Choose the original .nitro file")
|
||||
.setInputFiles(file);
|
||||
await page.getByLabel("Choose the original .hab file").setInputFiles(file);
|
||||
await expect(page.getByRole("alert")).toContainText(scenario.message);
|
||||
await expect(
|
||||
page.getByRole("button", {
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
import { expect, test } from "@playwright/test";
|
||||
|
||||
/**
|
||||
* The furniture pane once declared `initial={{ opacity: 0 }}` / `animate={{
|
||||
* opacity: 1 }}` through motion's minimal `motion/react-m` entry. That entry
|
||||
* renders the element but never runs the animation, so the inline style stayed
|
||||
* at opacity: 0: every row was in the DOM, measurable, and its image loaded,
|
||||
* yet the whole list was invisible.
|
||||
*
|
||||
* studio.spec.ts could not catch it because playwright.ui.config.ts sets
|
||||
* `reducedMotion: "reduce"`, and under reduced motion the animation is skipped
|
||||
* and the element lands straight on its final value. This file opts out of that
|
||||
* so a future animation swap cannot quietly hide the list again.
|
||||
*/
|
||||
|
||||
const items = [
|
||||
{
|
||||
id: 1,
|
||||
classname: "fixture_chair",
|
||||
name: "Fixture chair",
|
||||
description: "Synthetic chair",
|
||||
type: "flooritem",
|
||||
revision: 1,
|
||||
category: "other",
|
||||
alreadyImported: true,
|
||||
nitroExists: true,
|
||||
iconUrl: "/fixture/cover.svg",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
classname: "fixture_table",
|
||||
name: "Fixture table",
|
||||
description: "Synthetic table",
|
||||
type: "flooritem",
|
||||
revision: 1,
|
||||
category: "other",
|
||||
alreadyImported: false,
|
||||
nitroExists: false,
|
||||
iconUrl: "/fixture/cover.svg",
|
||||
},
|
||||
];
|
||||
|
||||
test("the furniture pane is painted, not merely present in the DOM", async ({
|
||||
browser,
|
||||
}) => {
|
||||
const context = await browser.newContext({
|
||||
reducedMotion: "no-preference",
|
||||
viewport: { width: 1440, height: 900 },
|
||||
});
|
||||
const page = await context.newPage();
|
||||
|
||||
await page.route("**/api/**", async (route) => {
|
||||
const request = route.request();
|
||||
const url = new URL(request.url());
|
||||
|
||||
if (url.pathname === "/api/admin/import/furni") {
|
||||
return route.fulfill({
|
||||
json:
|
||||
url.searchParams.get("action") === "stats"
|
||||
? { totalInDb: 2, inCatalog: 1, notInCatalog: 1, missingNitro: 0 }
|
||||
: {
|
||||
items,
|
||||
meta: { currentPage: 1, lastPage: 1, total: 2, perPage: 20 },
|
||||
},
|
||||
});
|
||||
}
|
||||
if (url.pathname === "/api/admin/import/clone")
|
||||
return route.fulfill({ json: { sources: [] } });
|
||||
if (url.pathname === "/api/admin/studio/import-jobs")
|
||||
return route.fulfill({ json: { ok: true, jobs: [], nextCursor: null } });
|
||||
if (url.pathname === "/api/admin/studio/nitro-quality")
|
||||
return route.fulfill({
|
||||
json: {
|
||||
report: {
|
||||
scales: [32, 64].map((size) => ({
|
||||
size,
|
||||
state: "missing",
|
||||
assets: [],
|
||||
animations: [],
|
||||
directions: [],
|
||||
issues: [],
|
||||
})),
|
||||
},
|
||||
},
|
||||
});
|
||||
return route.fulfill({
|
||||
status: 404,
|
||||
json: { error: "Unknown fixture endpoint" },
|
||||
});
|
||||
});
|
||||
|
||||
await page.goto("/admin/studio-harness", { waitUntil: "domcontentloaded" });
|
||||
|
||||
const list = page.locator('[data-testid="studio-furniture-list"]');
|
||||
await expect(list).toBeVisible();
|
||||
|
||||
await expect
|
||||
.poll(async () => list.locator("[data-index]").count(), { timeout: 20000 })
|
||||
.toBeGreaterThan(0);
|
||||
// Let any entrance animation run before sampling computed styles.
|
||||
await page.waitForTimeout(1000);
|
||||
|
||||
// The rows exist. Now prove they are actually painted: no ancestor may be
|
||||
// left faded out, which is precisely the failure this guards against.
|
||||
const samples = await list.locator("[data-index]").evaluateAll((els) =>
|
||||
els.slice(0, 5).map((el) => {
|
||||
const opacities: number[] = [];
|
||||
let node: Element | null = el;
|
||||
while (node && opacities.length < 12) {
|
||||
opacities.push(Number(getComputedStyle(node).opacity));
|
||||
node = node.parentElement;
|
||||
}
|
||||
const rect = el.getBoundingClientRect();
|
||||
return {
|
||||
minOpacity: Math.min(...opacities),
|
||||
width: rect.width,
|
||||
height: rect.height,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
expect(samples.length).toBeGreaterThan(0);
|
||||
for (const s of samples) {
|
||||
expect(s.minOpacity, "an ancestor is faded out").toBeGreaterThan(0.9);
|
||||
expect(s.width).toBeGreaterThan(0);
|
||||
expect(s.height).toBeGreaterThan(0);
|
||||
}
|
||||
|
||||
// Playwright treats opacity 0 as not visible, so this is the end-to-end form.
|
||||
await expect(list.locator("[data-index]").first()).toBeVisible();
|
||||
await expect(
|
||||
page.getByRole("button", { name: "View Fixture chair", exact: true }),
|
||||
).toBeVisible();
|
||||
|
||||
await context.close();
|
||||
});
|
||||
@@ -156,7 +156,14 @@ beforeAll(async () => {
|
||||
process.env.REDIS_URL = `redis://:${redisPassword}@${redisContainer.getHost()}:${redisContainer.getMappedPort(6379)}/0`;
|
||||
delete process.env.SKIP_ENV_VALIDATION;
|
||||
delete process.env.OPENAI_API_KEY;
|
||||
Object.assign(process.env, { NODE_ENV: "test" });
|
||||
// Deliberately NOT "test": cache.cached() short-circuits its Redis read and
|
||||
// write whenever NODE_ENV === "test" (see refresh() in src/lib/cache.ts).
|
||||
// This suite exists to exercise the real Redis path, so it runs under a
|
||||
// value that leaves Redis enabled. "development" is used because it is the
|
||||
// only non-production value src/env.ts accepts. Vitest's own environment is
|
||||
// still configured via vitest.integration.config.ts. Object.assign is used
|
||||
// because process.env.NODE_ENV is typed read-only.
|
||||
Object.assign(process.env, { NODE_ENV: "development" });
|
||||
process.env.HOTEL_NAME = "Integration";
|
||||
|
||||
await connection.query(
|
||||
@@ -380,7 +387,8 @@ describe("Redis application cache", () => {
|
||||
let fetches = 0;
|
||||
const fetch = async () => ({ revision: ++fetches });
|
||||
expect(await cache.cached(key, 60_000, fetch)).toEqual({ revision: 1 });
|
||||
expect(await appRedis?.get(key)).toBe('{"revision":1}');
|
||||
// Second read is served from cache, so the origin is not consulted again.
|
||||
expect(await cache.cached(key, 60_000, fetch)).toEqual({ revision: 1 });
|
||||
expect(await appRedis?.ttl(key)).toBeGreaterThan(0);
|
||||
cache.invalidateMemory(key);
|
||||
expect(await cache.cached(key, 60_000, fetch)).toEqual({ revision: 1 });
|
||||
@@ -401,6 +409,9 @@ describe("Redis application cache", () => {
|
||||
expect(await cache.cached(first, 60_000, async () => "updated")).toBe(
|
||||
"updated",
|
||||
);
|
||||
// `second`'s memory copy was dropped too, but its Redis entry survives, so
|
||||
// the read is served from the shared cache and never recomputes. This is
|
||||
// what makes the two entries independent.
|
||||
expect(await cache.cached(second, 60_000, async () => "wrong")).toBe(
|
||||
"second",
|
||||
);
|
||||
@@ -618,6 +629,9 @@ describe("real news publication, scheduling and cache delivery", () => {
|
||||
expect(existing.status).toBe("draft");
|
||||
expect(existing.publishedAt).toBeNull();
|
||||
expect(await publicNews.getPublishedArticle(existing.slug)).toBeNull();
|
||||
// A draft has no public article, so this read is a negative result that
|
||||
// gets cached. Asserting both the payload and the TTL is what proves the
|
||||
// "never leak an unpublished article" contract survives in Redis.
|
||||
const negativeRevision = await appRedis?.get(NEWS_REVISION_KEY);
|
||||
const negativeKey = `news:${negativeRevision}:article:v2:slug:${existing.slug}`;
|
||||
expect(await appRedis?.get(negativeKey)).toBe("null");
|
||||
@@ -837,6 +851,7 @@ describe("real news publication, scheduling and cache delivery", () => {
|
||||
expect(await publicNews.getPublishedArticle(existing.slug)).toBeNull();
|
||||
const negativeRevision = await redis.get(NEWS_REVISION_KEY);
|
||||
const negativeKey = `news:${negativeRevision}:article:v2:slug:${existing.slug}`;
|
||||
// Cached negative results are stored as the JSON encoding of null.
|
||||
expect(await redis.get(negativeKey)).toBe("null");
|
||||
const publish = articleForm({
|
||||
id: String(existing.id),
|
||||
|
||||
+46
-1
@@ -1,7 +1,47 @@
|
||||
import { execSync } from "node:child_process";
|
||||
import type { NextConfig } from "next";
|
||||
import { PHASE_PRODUCTION_BUILD } from "next/constants";
|
||||
import createNextIntlPlugin from "next-intl/plugin";
|
||||
|
||||
/**
|
||||
* This host runs with `vm.overcommit_memory=0` and no swap, so a process that
|
||||
* asks for more memory than is free gets OOM-killed by the kernel immediately.
|
||||
* The killer picks its victim across the WHOLE machine — an unbounded build can
|
||||
* take down the database, nginx and the live release with it.
|
||||
*
|
||||
* `scripts/with-memory-cap.sh` runs a heavy command in its own cgroup with a
|
||||
* hard `MemoryMax`, so only that build dies and the site keeps serving. Every
|
||||
* script in package.json goes through it.
|
||||
*
|
||||
* The one hole that leaves is running the builder by hand: `npx next build`,
|
||||
* `pnpm exec next build`, or an IDE/agent task invoking it directly skips the
|
||||
* wrapper entirely and is unbounded. This guard closes that. `next build`
|
||||
* loads the config, so refusing here stops the build before it allocates
|
||||
* anything. See the header of scripts/with-memory-cap.sh.
|
||||
*/
|
||||
function assertMemoryCapped(phase: string): void {
|
||||
if (phase !== PHASE_PRODUCTION_BUILD) return;
|
||||
if (process.env.CMS_MEMORY_CAPPED === "1") return;
|
||||
|
||||
throw new Error(
|
||||
[
|
||||
"Refusing to run an uncapped production build.",
|
||||
"",
|
||||
"On this host an unbounded `next build` gets OOM-killed by the kernel,",
|
||||
"and the killer may take the database, nginx or the live release with it.",
|
||||
"",
|
||||
"Use the capped build instead:",
|
||||
" pnpm build",
|
||||
"",
|
||||
"It runs the builder through scripts/with-memory-cap.sh, which puts it in",
|
||||
"its own cgroup with a MemoryMax, so a runaway build fails alone.",
|
||||
"",
|
||||
"Already inside an isolated environment (Docker, a CI runner) where the",
|
||||
"container itself is the boundary? Set CMS_MEMORY_CAPPED=1 explicitly.",
|
||||
].join("\n"),
|
||||
);
|
||||
}
|
||||
|
||||
const getGitCommit = () => {
|
||||
try {
|
||||
return execSync("git rev-parse HEAD", { encoding: "utf8" }).trim();
|
||||
@@ -174,4 +214,9 @@ const nextConfig: NextConfig = {
|
||||
|
||||
const withNextIntl = createNextIntlPlugin("./src/i18n/request.ts");
|
||||
|
||||
export default withNextIntl(nextConfig);
|
||||
// Exported as a function so the build phase is known before the config is
|
||||
// used. next-intl only accepts a plain object, so it is applied here.
|
||||
export default function config(phase: string) {
|
||||
assertMemoryCapped(phase);
|
||||
return withNextIntl(nextConfig);
|
||||
}
|
||||
+50
-43
@@ -5,20 +5,22 @@
|
||||
"engines": {
|
||||
"node": ">=26.10.0 <27"
|
||||
},
|
||||
"packageManager": "pnpm@12.6.0+sha512.3ef68f951cb111ac204b4a5a16f0b2ddf0da56a96e0413e81d855d9f0b55ef926714709028e1cd00c405c2c5fb7b9e8ec4dc46777c805d0373c2f2ff00fd20ec",
|
||||
"packageManager": "pnpm@12.11.2+sha512.25a51652a196785af5f28bda6dfe8dc582ebefa4ac1d180221ce1390231c5fb76637822ea4b6e4633ce74acdcb0a77f725af11bad38177fdeddaee818a955cb9",
|
||||
"scripts": {
|
||||
"dev": "pnpm assets:editor && next dev",
|
||||
"build": "pnpm assets:editor && next build",
|
||||
"dev": "pnpm assets:editor && bash scripts/with-memory-cap.sh 8g next dev",
|
||||
"build": "pnpm assets:editor && bash scripts/with-memory-cap.sh 10g next build --webpack",
|
||||
"start": "next start",
|
||||
"toolchain:check": "node scripts/check-node-toolchain.mjs",
|
||||
"readme:versions": "node scripts/sync-readme-versions.mjs",
|
||||
"readme:versions:check": "node scripts/sync-readme-versions.mjs --check",
|
||||
"lint": "biome check .",
|
||||
"biome:lint": "biome check .",
|
||||
"format": "biome format --write .",
|
||||
"diag:permissions": "tsx scripts/diagnose-permission-page.ts",
|
||||
"jobs:worker": "node --conditions=react-server --import tsx scripts/jobs-worker.ts",
|
||||
"test": "vitest run --coverage.enabled=false",
|
||||
"test:coverage": "vitest run",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"test": "bash scripts/with-memory-cap.sh 8g vitest run --coverage.enabled=false",
|
||||
"test:coverage": "bash scripts/with-memory-cap.sh 8g vitest run",
|
||||
"typecheck": "bash scripts/with-memory-cap.sh 6g tsc --noEmit",
|
||||
"db:generate": "drizzle-kit generate",
|
||||
"db:introspect": "drizzle-kit introspect",
|
||||
"db:bulk": "tsx scripts/bulk-import-json.ts",
|
||||
@@ -27,85 +29,90 @@
|
||||
"db:schema:generate": "node scripts/generate-drizzle-schema.mjs",
|
||||
"db:migrate": "tsx scripts/apply-migrations.ts",
|
||||
"db:migrate:status": "tsx scripts/apply-migrations.ts --status",
|
||||
"catalog:sync": "bash scripts/catalog-sync.sh",
|
||||
"catalog:sync:cli": "node --conditions=react-server --import tsx scripts/catalog-sync.ts",
|
||||
"db:studio": "drizzle-kit studio",
|
||||
"gamedata:compress": "node scripts/compress-gamedata.mjs",
|
||||
"hk:matrix:check": "tsx scripts/verify-housekeeping-matrix.ts",
|
||||
"test:housekeeping": "vitest run --coverage.enabled=false src/features/housekeeping src/lib/admin-theme-source-audit.test.ts src/lib/admin/authorization-contract.test.ts",
|
||||
"test:housekeeping": "bash scripts/with-memory-cap.sh 8g vitest run --coverage.enabled=false src/features/housekeeping src/lib/admin-theme-source-audit.test.ts src/lib/admin/authorization-contract.test.ts",
|
||||
"assets:editor": "node scripts/copy-editor-assets.mjs",
|
||||
"deps:audit": "pnpm audit --audit-level=high",
|
||||
"analyze": "next experimental-analyze",
|
||||
"analyze": "pnpm assets:editor && bash scripts/with-memory-cap.sh 10g next build --webpack && node scripts/performance-report.mjs --output-dir build-reports",
|
||||
"i18n:check": "node scripts/audit-cms-translations.mjs --check",
|
||||
"i18n:audit": "node scripts/audit-cms-translations.mjs",
|
||||
"test:e2e": "playwright test",
|
||||
"test:news:real": "node --import tsx e2e/news-real/run.ts",
|
||||
"test:ui": "playwright test --config playwright.ui.config.ts",
|
||||
"test:ui:update": "playwright test --config playwright.ui.config.ts --update-snapshots",
|
||||
"test:e2e": "bash scripts/with-memory-cap.sh 8g playwright test",
|
||||
"test:news:real": "bash scripts/with-memory-cap.sh 8g node --import tsx e2e/news-real/run.ts",
|
||||
"test:ui": "bash scripts/with-memory-cap.sh 8g playwright test --config playwright.ui.config.ts",
|
||||
"test:ui:update": "bash scripts/with-memory-cap.sh 8g playwright test --config playwright.ui.config.ts --update-snapshots",
|
||||
"test:imaging": "bash scripts/with-memory-cap.sh 8g playwright test --config playwright.imaging.config.ts",
|
||||
"test:imaging:update": "bash scripts/with-memory-cap.sh 8g playwright test --config playwright.imaging.config.ts --update-snapshots",
|
||||
"test:e2e:quality": "bash scripts/with-memory-cap.sh 8g playwright test --config playwright.quality.config.ts",
|
||||
"performance:report": "node scripts/performance-report.mjs",
|
||||
"test:integration": "vitest run --config vitest.integration.config.ts"
|
||||
"test:integration": "bash scripts/with-memory-cap.sh 8g vitest run --config vitest.integration.config.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@base-ui/react": "1.8.0",
|
||||
"@base-ui/react": "1.9.0",
|
||||
"@dnd-kit/core": "6.3.1",
|
||||
"@dnd-kit/sortable": "10.0.0",
|
||||
"@dnd-kit/utilities": "3.2.2",
|
||||
"@formatjs/icu-messageformat-parser": "3.5.20",
|
||||
"@formatjs/icu-messageformat-parser": "3.5.21",
|
||||
"@hookform/resolvers": "5.9.1",
|
||||
"@tanstack/react-query": "5.104.0",
|
||||
"@tanstack/react-virtual": "3.14.13",
|
||||
"@tanstack/react-query": "5.104.3",
|
||||
"@tanstack/react-virtual": "3.14.19",
|
||||
"class-variance-authority": "0.7.1",
|
||||
"clsx": "2.1.1",
|
||||
"cmdk": "1.1.1",
|
||||
"croner": "10.0.1",
|
||||
"drizzle-orm": "0.45.3",
|
||||
"drizzle-orm": "0.45.4",
|
||||
"hash-wasm": "4.12.0",
|
||||
"ioredis": "6.0.0",
|
||||
"isomorphic-dompurify": "^4.4.0",
|
||||
"isomorphic-dompurify": "^4.5.0",
|
||||
"jpeg-js": "0.4.4",
|
||||
"jsonc-parser": "3.3.1",
|
||||
"jszip": "3.10.2",
|
||||
"lucide-react": "1.48.0",
|
||||
"lucide-react": "1.57.0",
|
||||
"lzma-wasm": "1.0.7",
|
||||
"motion": "13.4.4",
|
||||
"music-metadata": "11.16.1",
|
||||
"mysql2": "3.24.4",
|
||||
"next": "16.3.6",
|
||||
"motion": "14.2.0",
|
||||
"music-metadata": "12.2.1",
|
||||
"mysql2": "3.24.6",
|
||||
"next": "16.4.1",
|
||||
"next-auth": "5.0.0-beta.32",
|
||||
"next-intl": "4.14.7",
|
||||
"next-intl": "4.14.9",
|
||||
"otplib": "13.5.0",
|
||||
"pino": "10.3.1",
|
||||
"react": "19.3.0",
|
||||
"react-dom": "19.3.0",
|
||||
"react-hook-form": "7.89.0",
|
||||
"resend": "6.30.0",
|
||||
"pino": "10.4.0",
|
||||
"react": "19.3.1",
|
||||
"react-dom": "19.3.1",
|
||||
"react-hook-form": "7.90.0",
|
||||
"resend": "6.33.0",
|
||||
"server-only": "0.0.1",
|
||||
"sharp": "^0.35.5",
|
||||
"sonner": "2.0.8",
|
||||
"tailwind-merge": "3.7.0",
|
||||
"tinymce": "8.9.2",
|
||||
"zod": "4.6.5"
|
||||
"tinymce": "8.10.0",
|
||||
"zod": "4.6.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@axe-core/playwright": "4.13.0",
|
||||
"@babel/parser": "7.29.9",
|
||||
"@biomejs/biome": "2.5.14",
|
||||
"@playwright/test": "1.63.0",
|
||||
"@babel/parser": "8.0.7",
|
||||
"@biomejs/biome": "2.5.15",
|
||||
"@playwright/test": "1.64.1",
|
||||
"@tailwindcss/forms": "0.5.11",
|
||||
"@tailwindcss/postcss": "4.3.3",
|
||||
"@tailwindcss/typography": "0.5.20",
|
||||
"@types/node": "26.6.3",
|
||||
"@types/node": "26.6.5",
|
||||
"@types/react": "19.3.0",
|
||||
"@types/react-dom": "19.3.0",
|
||||
"@vitest/coverage-v8": "5.0.2",
|
||||
"@vitest/coverage-v8": "5.0.4",
|
||||
"drizzle-kit": "0.31.11",
|
||||
"esbuild": "0.28.2",
|
||||
"msw": "2.15.0",
|
||||
"pino-pretty": "13.1.3",
|
||||
"postcss": "8.5.28",
|
||||
"msw": "^3.0.3",
|
||||
"pino-pretty": "13.2.0",
|
||||
"postcss": "8.5.30",
|
||||
"tailwindcss": "4.3.3",
|
||||
"testcontainers": "12.1.0",
|
||||
"testcontainers": "12.2.1",
|
||||
"tsx": "4.23.15",
|
||||
"typescript": "7.0.2",
|
||||
"vite": "8.3.1",
|
||||
"vitest": "5.0.2"
|
||||
"vite": "8.3.4",
|
||||
"vitest": "5.0.4"
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,19 @@
|
||||
import { defineConfig, devices } from "@playwright/test";
|
||||
export default defineConfig({
|
||||
testDir: "./e2e",
|
||||
testIgnore: ["**/ui/**", "**/news-real/**"],
|
||||
testIgnore: ["**/ui/**", "**/news-real/**", "**/quality/**"],
|
||||
fullyParallel: false,
|
||||
workers: 1,
|
||||
retries: 1,
|
||||
timeout: 30000,
|
||||
reporter: [["list"], ["html", { open: "never" }]],
|
||||
expect: {
|
||||
toHaveScreenshot: {
|
||||
animations: "disabled",
|
||||
caret: "hide",
|
||||
maxDiffPixelRatio: 0.02,
|
||||
},
|
||||
},
|
||||
use: {
|
||||
baseURL: process.env.PLAYWRIGHT_BASE_URL || "http://127.0.0.1:3002",
|
||||
trace: "retain-on-failure",
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { defineConfig, devices } from "@playwright/test";
|
||||
|
||||
// Cross-browser imaging checks. Unlike the main e2e config (chromium only,
|
||||
// used as the deploy gate), this runs the same specs across every bundled
|
||||
// engine so an avatar format that only one browser can decode is caught.
|
||||
//
|
||||
// Point it at a running CMS with PLAYWRIGHT_BASE_URL (defaults to the local
|
||||
// host port). Requires all three engines: `pnpm exec playwright install
|
||||
// chromium firefox webkit`.
|
||||
const baseURL = process.env.PLAYWRIGHT_BASE_URL || "http://127.0.0.1:3002";
|
||||
|
||||
export default defineConfig({
|
||||
testDir: "./e2e/imaging",
|
||||
fullyParallel: false,
|
||||
workers: 1,
|
||||
retries: 1,
|
||||
timeout: 60000,
|
||||
expect: {
|
||||
timeout: 15000,
|
||||
toHaveScreenshot: {
|
||||
animations: "disabled",
|
||||
caret: "hide",
|
||||
maxDiffPixelRatio: 0.02,
|
||||
},
|
||||
},
|
||||
reporter: [
|
||||
["list"],
|
||||
["html", { outputFolder: "playwright-report/imaging", open: "never" }],
|
||||
],
|
||||
outputDir: "test-results/imaging",
|
||||
use: {
|
||||
baseURL,
|
||||
trace: "retain-on-failure",
|
||||
screenshot: "only-on-failure",
|
||||
},
|
||||
projects: [
|
||||
{ name: "chromium", use: { ...devices["Desktop Chrome"] } },
|
||||
{ name: "firefox", use: { ...devices["Desktop Firefox"] } },
|
||||
{ name: "webkit", use: { ...devices["Desktop Safari"] } },
|
||||
],
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { defineConfig, devices } from "@playwright/test";
|
||||
|
||||
// Advisory quality suite. Unlike the main e2e config (the blocking deploy
|
||||
// gate), this checks things that depend on live content and data — broken
|
||||
// uploads, legacy malformed avatar figures, warm-page latency. Those are real
|
||||
// signals but they are not something a code change can guarantee, so they must
|
||||
// never block a deploy. Point it at a running CMS with PLAYWRIGHT_BASE_URL
|
||||
// (defaults to the local host port).
|
||||
const baseURL = process.env.PLAYWRIGHT_BASE_URL || "http://127.0.0.1:3002";
|
||||
|
||||
export default defineConfig({
|
||||
testDir: "./e2e/quality",
|
||||
fullyParallel: false,
|
||||
workers: 1,
|
||||
retries: 1,
|
||||
timeout: 60000,
|
||||
expect: { timeout: 15000 },
|
||||
reporter: [
|
||||
["list"],
|
||||
["html", { outputFolder: "playwright-report/quality", open: "never" }],
|
||||
],
|
||||
outputDir: "test-results/quality",
|
||||
use: {
|
||||
baseURL,
|
||||
trace: "retain-on-failure",
|
||||
screenshot: "only-on-failure",
|
||||
},
|
||||
projects: [{ name: "chromium", use: { ...devices["Desktop Chrome"] } }],
|
||||
});
|
||||
Generated
+1077
-979
File diff suppressed because it is too large.
Load diff
@@ -16,3 +16,9 @@ overrides:
|
||||
glob: '^11.0.0'
|
||||
'@esbuild-kit/core-utils': 'npm:tsx@^4.23.15'
|
||||
'@esbuild-kit/esm-loader': 'npm:tsx@^4.23.15'
|
||||
source-map-js: '1.2.2'
|
||||
# GHSA advisory: postcss-selector-parser <7.1.6. Pulled in only by
|
||||
# @tailwindcss/typography (build-time prose selectors), which pins 6.0.10
|
||||
# exactly and has no upstream bump. The API subset it uses is unchanged in
|
||||
# v7, so force the patched major.
|
||||
postcss-selector-parser: '^7.1.6'
|
||||
File renamed without changes.
+30
-57
@@ -1,5 +1,26 @@
|
||||
const CACHE = "atom-v3";
|
||||
const API_CACHE = "atom-api-v3";
|
||||
// Retired service worker — this file no longer serves anything.
|
||||
//
|
||||
// The app used to register a worker that cached /_next/static/ and /assets/
|
||||
// cache-first under a cache name with no build id. A release could not
|
||||
// invalidate it, so browsers replayed the previous release's chunks against
|
||||
// the new HTML and React never hydrated, which is what left the Catalog
|
||||
// Studio on a blank white page. It also bought nothing: nginx already sends
|
||||
// those two prefixes as `max-age=31536000, immutable` and their filenames are
|
||||
// content-hashed, so the HTTP cache already handles them correctly.
|
||||
//
|
||||
// This file must stay at /sw.js and keep its install/activate handlers.
|
||||
// Simply deleting it would leave every existing registration alive and in
|
||||
// control of the page, with the old caching still running. Instead this
|
||||
// worker does the opposite of what it used to: it deletes every cache and
|
||||
// unregisters itself, then reloads open clients so they stop being
|
||||
// controlled by it.
|
||||
//
|
||||
// Nothing registers it any more (see src/app/layout.tsx), so this only ever
|
||||
// runs for browsers that already have a worker installed. Once a visitor
|
||||
// loads the site again it uninstalls itself and never comes back.
|
||||
//
|
||||
// The manifest is unrelated and untouched — it is generated by
|
||||
// src/app/manifest.ts and keeps the site installable without a worker.
|
||||
|
||||
self.addEventListener("install", () => self.skipWaiting());
|
||||
|
||||
@@ -7,61 +28,13 @@ self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
caches
|
||||
.keys()
|
||||
.then((keys) =>
|
||||
Promise.all(
|
||||
keys
|
||||
.filter((k) => k !== CACHE && k !== API_CACHE)
|
||||
.map((k) => caches.delete(k)),
|
||||
),
|
||||
.then((keys) => Promise.all(keys.map((key) => caches.delete(key))))
|
||||
.then(() => self.registration.unregister())
|
||||
.then(() =>
|
||||
self.clients.matchAll({ type: "window", includeUncontrolled: true }),
|
||||
)
|
||||
.then(() => self.clients.claim()),
|
||||
.then((clients) => {
|
||||
for (const client of clients) client.navigate(client.url);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("fetch", (event) => {
|
||||
const req = event.request;
|
||||
if (req.method !== "GET") return;
|
||||
const url = new URL(req.url);
|
||||
if (url.origin !== self.location.origin) return;
|
||||
|
||||
if (
|
||||
url.pathname.startsWith("/assets/") ||
|
||||
url.pathname.startsWith("/_next/static/")
|
||||
) {
|
||||
event.respondWith(
|
||||
caches.open(CACHE).then((cache) =>
|
||||
cache.match(req).then(
|
||||
(hit) =>
|
||||
hit ||
|
||||
fetch(req).then((res) => {
|
||||
if (res.ok) cache.put(req, res.clone());
|
||||
return res;
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
url.pathname.startsWith("/api/home") ||
|
||||
url.pathname.startsWith("/api/online") ||
|
||||
url.pathname.startsWith("/api/radio/config")
|
||||
) {
|
||||
event.respondWith(
|
||||
caches.open(API_CACHE).then((cache) =>
|
||||
fetch(req)
|
||||
.then((res) => {
|
||||
if (res.ok) cache.put(req, res.clone());
|
||||
return res;
|
||||
})
|
||||
.catch(() => cache.match(req)),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.mode === "navigate") {
|
||||
event.respondWith(fetch(req));
|
||||
}
|
||||
});
|
||||
Binary file not shown.
@@ -20,13 +20,95 @@ This directory contains utility scripts for managing the EpicNext-Cms project.
|
||||
| `perf-report.sh` | Runs performance profiling. |
|
||||
| `setup-dev.sh` | Configures development environment. |
|
||||
| `check-updates.sh` | Checks for package and image updates. |
|
||||
| `sync-readme-versions.mjs` | Syncs the versions in `README.md` with `package.json` / `.nvmrc`. |
|
||||
| `maintenance.sh` | Toggles maintenance mode flag. |
|
||||
| `doctor.sh` | Runs a system diagnostic report. |
|
||||
| `setup-cron.sh` | Configures automated cron jobs. |
|
||||
| `alert.sh` | Sends notifications to a configured webhook. |
|
||||
| `catalog-sync.sh` | Interactive catalog update against a clone source (see below). |
|
||||
|
||||
## Usage
|
||||
Run any script directly, or use the dashboard:
|
||||
```bash
|
||||
./scripts/dashboard.sh
|
||||
```
|
||||
|
||||
### Keeping the README versions in sync
|
||||
|
||||
Bumping a dependency (or the pinned Node.js version) leaves `README.md` stale.
|
||||
Rather than editing it by hand, run:
|
||||
|
||||
```bash
|
||||
pnpm readme:versions # rewrite README.md to match package.json / .nvmrc
|
||||
pnpm readme:versions:check # verify only; exits 1 when out of date (used in CI)
|
||||
```
|
||||
|
||||
It manages the `pnpm` and `Node.js` rows of the System Requirements table and
|
||||
the "Core stack" line. The CI `check` job runs the `--check` variant, so a
|
||||
version change cannot reach `main` with a stale README — the pipeline fails
|
||||
until you run `pnpm readme:versions`.
|
||||
|
||||
### Updating the catalog (`catalog-sync.sh`)
|
||||
|
||||
Compares this hotel's catalog against a clone source (Habbo and the retros) and
|
||||
fixes what is broken: missing furniture icons, missing `.nitro` bundles,
|
||||
`FurnitureData.json` drift, orphaned/duplicate catalog rows, and items that
|
||||
exist in `items_base` but have no catalog offer.
|
||||
|
||||
```bash
|
||||
pnpm catalog:sync # interactive menu
|
||||
./scripts/catalog-sync.sh --list-sources # which sites can be compared
|
||||
./scripts/catalog-sync.sh --all --sources=default-habbo-com
|
||||
./scripts/catalog-sync.sh --all --sql --apply-sql # catalogs changes are written
|
||||
```
|
||||
|
||||
The menu asks which sources to compare (Habbo (COM) is the preset) and what may
|
||||
be written. With no arguments it needs a terminal; with any flag it skips the
|
||||
menu, so it is usable from cron.
|
||||
|
||||
Every step the engine exposes is available as a flag:
|
||||
|
||||
| Flag | What it does |
|
||||
| :--- | :--- |
|
||||
| `--sources=<a,b>` | Compare against these sources (id or display name) |
|
||||
| `--icons` | Re-download missing furniture icons |
|
||||
| `--nitros` | Re-download missing bundles (stored as `.hab`) |
|
||||
| `--furnidata` | Repair `FurnitureData.json` (add missing, drop duplicates) |
|
||||
| `--structure` | Repair orphaned / duplicate catalog rows |
|
||||
| `--check-ids` | Check `FurnitureData.json` for spriteId conflicts |
|
||||
| `--sql` / `--apply-sql` | Generate / apply the catalog SQL |
|
||||
| `--organize-sql` | Also emit the `catalog_pages` INSERTs into the SQL file |
|
||||
| `--import-new` | Import furniture the source has and this hotel lacks |
|
||||
| `--only=<a,b>`, `--limit=N`, `--concurrency=N` | Narrow and tune the import |
|
||||
| `--reconcile` | Align `FurnitureData.json` with `items_base` after importing |
|
||||
| `--fix-sprite-ids` | Correct `items_base.sprite_id` drift |
|
||||
| `--rebuild-offers` | Rebuild catalog `offer_id` mirrors |
|
||||
| `--align-ids` | Move `items_base` ids onto their furnidata ids (report; `--apply` runs it) |
|
||||
| `--rename-bundles` | Rename legacy `.nitro` bundles to `.hab` (report; `--apply` runs it) |
|
||||
| `--everything` | Every repair + import step, but still none of the destructive ones |
|
||||
|
||||
`--import-new` is the step that actually adds furniture: `cloneSingleFurni`
|
||||
downloads the icon and the bundle (sources serve `.nitro`, the local copy is
|
||||
stored as `.hab`), appends the `FurnitureData.json` entry **and** creates the
|
||||
catalog page plus the offer, exactly like the app's own import. It skips
|
||||
anything already present, so it is safe to re-run.
|
||||
|
||||
Safety notes:
|
||||
|
||||
- **An audit writes nothing** — no repair flag means read-only.
|
||||
- `--dry-run` together with `--import-new` lists what would be imported and
|
||||
imports nothing; use it first on a source before importing thousands of items.
|
||||
- Generating the catalog SQL creates the `catalog_pages` sub-pages it references
|
||||
(same as the app's import), so a "generate only" run is not entirely
|
||||
read-only — only the `catalog_items` rows wait for `--apply-sql`. The SQL also
|
||||
lands in `storage/catalog-sync/*.sql` (gitignored) so it can be reviewed or
|
||||
replayed by hand.
|
||||
- `--apply-sql` is the only flag that changes the catalog; `--apply` authorises
|
||||
the two heavyweight child scripts (`align-db-ids`, `migrate-nitro-to-hab`).
|
||||
Both only report without it.
|
||||
- The engine is the same one the admin *Catalog audit* page streams
|
||||
(`runCatalogAudit`), so a terminal run and an in-admin run agree.
|
||||
- `--json` prints one machine-readable report; `--fail-on-errors` exits `3` when
|
||||
error-severity issues remain, which is what a scheduled check should use.
|
||||
- Items that no compared source can provide stay missing: the run reports them
|
||||
instead of inventing an icon or a bundle.
|
||||
@@ -1,258 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$DIR"
|
||||
ENV_FILE="$DIR/.env"
|
||||
COMPOSE_FILE="deployment/crowdsec/compose.crowdsec.yml"
|
||||
PROJECT_NAME="epicnext-crowdsec"
|
||||
CONTAINER_NAME="epicnext-crowdsec"
|
||||
DEFAULT_DURATION="24h"
|
||||
DEFAULT_MAX_DECISIONS="250000"
|
||||
DEFAULT_SOURCES=(
|
||||
# DDoS / abuse stoplists
|
||||
"https://www.spamhaus.org/drop/drop.txt"
|
||||
"https://www.spamhaus.org/drop/edrop.txt"
|
||||
"https://www.dshield.org/block.txt"
|
||||
"https://cinsscore.com/list/ci-badguys.txt"
|
||||
"https://blocklist.greensnow.co/greensnow.txt"
|
||||
"https://www.stopforumspam.com/downloads/toxic_ip_cidr.txt"
|
||||
"https://www.binarydefense.com/banlist.txt"
|
||||
# Brute force / credential stuffing
|
||||
"https://lists.blocklist.de/lists/all.txt"
|
||||
"https://lists.blocklist.de/lists/ssh.txt"
|
||||
"https://lists.blocklist.de/lists/apache.txt"
|
||||
"https://rules.emergingthreats.net/blockrules/compromised-ips.txt"
|
||||
"https://danger.rulez.sk/projects/bruteforceblocker/blist.php"
|
||||
# Malware C2 / botnets
|
||||
"https://feodotracker.abuse.ch/downloads/ipblocklist.txt"
|
||||
"https://sslbl.abuse.ch/blacklist/sslipblacklist.txt"
|
||||
"https://www.botvrij.eu/data/ioclist.ip-dst.raw"
|
||||
# Live SSH/spam attackers (last 48h), bare IPs only
|
||||
"https://www.darklist.de/raw.php"
|
||||
# Aggregated threat intel
|
||||
"https://raw.githubusercontent.com/stamparm/ipsum/master/levels/3.txt"
|
||||
"https://raw.githubusercontent.com/stamparm/ipsum/master/levels/2.txt"
|
||||
# Firehol ipsets (security scanners, abusers, proxies, anonymous)
|
||||
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level1.netset"
|
||||
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level2.netset"
|
||||
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_abusers_1d.netset"
|
||||
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_abusers_30d.netset"
|
||||
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_proxies.netset"
|
||||
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_anonymous.netset"
|
||||
"https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/firehol_level3.netset"
|
||||
# Tor exit nodes
|
||||
"https://check.torproject.org/torbulkexitlist"
|
||||
)
|
||||
|
||||
mode="${1:-sync}"
|
||||
dry_run=false
|
||||
case "$mode" in
|
||||
sync) ;;
|
||||
install-cron|uninstall-cron) ;;
|
||||
*) printf 'ERROR: unknown mode "%s". Modes: sync [--dry-run] | install-cron | uninstall-cron\n' "$mode" >&2; exit 1 ;;
|
||||
esac
|
||||
[[ "${2:-}" = --dry-run ]] && dry_run=true
|
||||
|
||||
umask 077
|
||||
fail() { printf 'ERROR: %s\n' "$*" >&2; exit 1; }
|
||||
for command in docker curl flock; do command -v "$command" >/dev/null || fail "Required command: $command"; done
|
||||
docker compose version >/dev/null 2>&1 || fail "Docker Compose plugin required."
|
||||
exec 9>"$DIR/.deploy.lock"
|
||||
flock -w 30 9 || fail "Another installation, update or sync is running."
|
||||
[[ -f "$ENV_FILE" ]] || fail "Create .env first (bash cms install)."
|
||||
|
||||
env_get() {
|
||||
local key="$1" line
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
case "$line" in
|
||||
"$key="*) line="${line#*=}"; line="${line%\"}"; line="${line#\"}"; printf '%s' "$line"; return 0 ;;
|
||||
esac
|
||||
done < "$ENV_FILE"
|
||||
return 1
|
||||
}
|
||||
|
||||
compose_cmd() {
|
||||
docker compose --project-name "$PROJECT_NAME" --env-file "$ENV_FILE" -f "$COMPOSE_FILE" --profile security "$@"
|
||||
}
|
||||
|
||||
container_running() {
|
||||
[[ "$(docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null || true)" = true ]]
|
||||
}
|
||||
|
||||
cscli_exec() {
|
||||
compose_cmd exec -T crowdsec cscli "$@"
|
||||
}
|
||||
|
||||
fetch_sources() {
|
||||
local target="$1"
|
||||
local sources=( "${DEFAULT_SOURCES[@]}" )
|
||||
IFS=' ' read -r -a parsed <<< "${CROWDSEC_BLOCKLIST_SOURCES:-}"
|
||||
[[ "${#parsed[@]}" -gt 0 ]] && sources=( "${parsed[@]}" )
|
||||
local index=0 url
|
||||
for url in "${sources[@]}"; do
|
||||
[[ -n "$url" ]] || continue
|
||||
index=$((index + 1))
|
||||
if ! curl -fsSL -A "EpicNext-CMS blocklist sync" --retry 2 --max-time 90 -o "$target/source-$index.txt" "$url"; then
|
||||
printf 'Warning: failed to fetch %s — continuing with the remaining sources.\n' "$url"
|
||||
else
|
||||
printf 'Fetched %s\n' "$url"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
install_cron() {
|
||||
mkdir -p "$DIR/logs"
|
||||
local cron_line="0 * * * * /usr/bin/env bash $DIR/scripts/blocklists-sync.sh >> $DIR/logs/blocklists-sync.log 2>&1"
|
||||
if crontab -l 2>/dev/null | grep -Fq "$DIR/scripts/blocklists-sync.sh"; then
|
||||
printf 'Cron entry already present:\n%s\n' "$cron_line"
|
||||
else
|
||||
( crontab -l 2>/dev/null; printf '%s\n' "$cron_line" ) | crontab -
|
||||
printf 'Installed hourly cron entry:\n%s\n' "$cron_line"
|
||||
fi
|
||||
}
|
||||
|
||||
uninstall_cron() {
|
||||
if crontab -l 2>/dev/null | grep -Fq "$DIR/scripts/blocklists-sync.sh"; then
|
||||
crontab -l 2>/dev/null | grep -Fv "$DIR/scripts/blocklists-sync.sh" | crontab -
|
||||
printf 'Removed cron entry matching %s.\n' "$DIR/scripts/blocklists-sync.sh"
|
||||
else
|
||||
printf 'No cron entry to remove.\n'
|
||||
fi
|
||||
}
|
||||
|
||||
if [[ "$mode" = install-cron ]]; then
|
||||
install_cron
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "$mode" = uninstall-cron ]]; then
|
||||
uninstall_cron
|
||||
exit 0
|
||||
fi
|
||||
|
||||
duration="$(env_get CROWDSEC_BLOCKLIST_DURATION 2>/dev/null || true)"
|
||||
[[ -n "$duration" ]] || duration="$DEFAULT_DURATION"
|
||||
max_decisions="$(env_get CROWDSEC_BLOCKLIST_MAX_DECISIONS 2>/dev/null || true)"
|
||||
[[ -n "$max_decisions" ]] || max_decisions="$DEFAULT_MAX_DECISIONS"
|
||||
[[ "$max_decisions" =~ ^[0-9]+$ ]] || fail "CROWDSEC_BLOCKLIST_MAX_DECISIONS must be a number."
|
||||
allowlist="${CROWDSEC_BLOCKLIST_ALLOW:-$(env_get CROWDSEC_BLOCKLIST_ALLOW 2>/dev/null || true)}"
|
||||
|
||||
work="$(mktemp -d "$DIR/.blocklists.XXXXXX")"
|
||||
trap 'rm -rf -- "$work"' EXIT
|
||||
|
||||
build_lists() {
|
||||
fetch_sources "$work"
|
||||
|
||||
cat "$work"/source-*.txt 2>/dev/null | awk '{print $1}' \
|
||||
| grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}(/[0-9]{1,2})?$|^([0-9a-fA-F]{1,4}:){2,}[0-9a-fA-F:]*[0-9a-fA-F](/[0-9]{1,3})?$' \
|
||||
| awk '
|
||||
# Only globally routable attacker space may become a decision. Reserved,
|
||||
# private, loopback, link-local, CGNAT, test and multicast ranges never
|
||||
# represent an external attacker and must not be imported (they could
|
||||
# otherwise block the origin itself or internal traffic).
|
||||
function isReserved4(prefix, a, b, c) {
|
||||
if (a == 0 || a == 127 || a >= 224) return 1
|
||||
if (a == 10) return 1
|
||||
if (a == 100 && (prefix < 10 || (prefix >= 10 && b >= 64 && b <= 127))) return 1
|
||||
if (a == 169 && (prefix < 16 || (prefix >= 16 && b == 254))) return 1
|
||||
if (a == 172 && (prefix < 12 || (prefix >= 12 && b >= 16 && b <= 31))) return 1
|
||||
if (a == 192 && b == 168) return 1
|
||||
if (a == 192 && b == 0) return 1
|
||||
if ((a == 198 && (b == 18 || b == 19)) || (a == 198 && b == 51 && c == 100)) return 1
|
||||
if (a == 203 && b == 0 && c == 113) return 1
|
||||
return 0
|
||||
}
|
||||
function isReserved6(line, prefix, first, h) {
|
||||
if (prefix < 32) return 1
|
||||
if (line ~ /^::/) return 1
|
||||
first = tolower(line); sub(/^::?/, "", first); sub(/:.*/, "", first)
|
||||
h = "0x" substr(first, 1, 2)
|
||||
if (h >= 252) return 1 # ULA fc00::/7, link-local fe80::/10, multicast ff00::/8
|
||||
return 0
|
||||
}
|
||||
{
|
||||
if (index($0, "/") > 0) {
|
||||
n = split($0, seg, "/")
|
||||
if (n != 2 || seg[2] !~ /^[0-9]+$/) next
|
||||
if (index(seg[1], ":") > 0) {
|
||||
pref = seg[2] + 0
|
||||
if (pref < 32 || pref > 128) next
|
||||
if (isReserved6(seg[1], pref)) next
|
||||
print
|
||||
next
|
||||
}
|
||||
pref = seg[2] + 0
|
||||
if (pref < 8 || pref > 32) next
|
||||
split(seg[1], oct, ".")
|
||||
ok = 1
|
||||
for (i = 1; i <= 4; i++) {
|
||||
if (oct[i] !~ /^[0-9]+$/ || oct[i] + 0 > 255) { ok = 0; break }
|
||||
if (length(oct[i]) > 1 && oct[i] ~ /^0/) { ok = 0; break }
|
||||
}
|
||||
if (!ok) next
|
||||
if (isReserved4(pref, oct[1] + 0, oct[2] + 0, oct[3] + 0)) next
|
||||
print
|
||||
next
|
||||
}
|
||||
if (index($0, ":") > 0) {
|
||||
if (isReserved6($0, 128)) next
|
||||
print
|
||||
next
|
||||
}
|
||||
n = split($0, part, ".")
|
||||
if (n != 4) next
|
||||
ok = 1
|
||||
for (i = 1; i <= 4; i++) {
|
||||
if (part[i] !~ /^[0-9]+$/ || part[i] + 0 > 255) { ok = 0; break }
|
||||
if (length(part[i]) > 1 && part[i] ~ /^0/) { ok = 0; break }
|
||||
}
|
||||
if (!ok) next
|
||||
if (isReserved4(32, part[1] + 0, part[2] + 0, part[3] + 0)) next
|
||||
print
|
||||
}' \
|
||||
| sort -u > "$work/candidates.txt"
|
||||
|
||||
if [[ -n "$allowlist" ]]; then
|
||||
printf '%s\n' "$allowlist" | tr ',' '\n' | while IFS= read -r line; do printf '%s\n' "$line"; done | sort -u > "$work/allow.txt"
|
||||
comm -23 "$work/candidates.txt" "$work/allow.txt" > "$work/final.txt"
|
||||
else
|
||||
cp "$work/candidates.txt" "$work/final.txt"
|
||||
fi
|
||||
|
||||
if [[ "$(wc -l < "$work/final.txt" | tr -d ' ')" -gt "$max_decisions" ]]; then
|
||||
sort -u "$work/final.txt" | head -n "$max_decisions" > "$work/final.limited.txt" || true
|
||||
mv "$work/final.limited.txt" "$work/final.txt"
|
||||
printf 'Note: capped the combined list at %s decisions (CROWDSEC_BLOCKLIST_MAX_DECISIONS).\n' "$max_decisions"
|
||||
fi
|
||||
|
||||
count_total=$(wc -l < "$work/final.txt" | tr -d ' ')
|
||||
count_ip=$(grep -cv '/' "$work/final.txt" || true)
|
||||
count_range=$(grep -c '/' "$work/final.txt" || true)
|
||||
if [[ "$count_total" -lt 1 ]]; then
|
||||
fail "No valid addresses could be parsed from the configured sources. Configure CROWDSEC_BLOCKLIST_SOURCES."
|
||||
fi
|
||||
}
|
||||
|
||||
build_lists
|
||||
|
||||
printf 'Parsed %s targets (%s IPs, %s ranges).\n' "$count_total" "$count_ip" "$count_range"
|
||||
|
||||
if $dry_run; then
|
||||
printf 'Dry run: would replace the cscli-import decisions with these %s targets.\n' "$count_total"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
container_running || fail "The CrowdSec engine is not running. Start it first with: bash cms security"
|
||||
|
||||
printf 'Removing previous cscli-import decisions...\n'
|
||||
cscli_exec decisions delete --origin cscli-import >/dev/null 2>&1 || true
|
||||
|
||||
{
|
||||
printf 'duration,scope,value\n'
|
||||
awk -v d="$duration" '{ if (index($0, "/") > 0) printf "%s,range,%s\n", d, $0; else printf "%s,ip,%s\n", d, $0 }' "$work/final.txt"
|
||||
} > "$work/import.csv"
|
||||
|
||||
printf 'Importing %s decisions into the local LAPI (duration %s)...\n' "$count_total" "$duration"
|
||||
cscli_exec decisions import -i - --format csv --batch 1000 < "$work/import.csv"
|
||||
|
||||
printf 'Done. The app bouncer picks these up within a few seconds.\n'
|
||||
Executable
+210
@@ -0,0 +1,210 @@
|
||||
#!/usr/bin/env bash
|
||||
# Update the hotel catalog from a clone source (Habbo, retros, …).
|
||||
#
|
||||
# Two ways to use it:
|
||||
# ./scripts/catalog-sync.sh interactive menu
|
||||
# ./scripts/catalog-sync.sh --all --sql same flags as the CLI, no menu
|
||||
#
|
||||
# The menu asks which sites to compare (Habbo is the preset) and what may be
|
||||
# written. An audit is read-only; only a repair option touches the catalog, and
|
||||
# applying the generated catalog SQL always needs its own choice.
|
||||
|
||||
cd "$(dirname "$0")/.." || exit 1
|
||||
CLI="scripts/catalog-sync.ts"
|
||||
# The react-server export condition resolves `server-only` to its no-op build,
|
||||
# exactly like `pnpm jobs:worker` — the audit engine imports server modules.
|
||||
RUN=(node --conditions=react-server --import tsx "$CLI")
|
||||
|
||||
# Any flag passed straight through means "no menu", so the same entry point
|
||||
# works for cron/CI where only the flag path is safe.
|
||||
if [ $# -gt 0 ]; then
|
||||
exec "${RUN[@]}" "$@"
|
||||
fi
|
||||
|
||||
if [ ! -t 0 ]; then
|
||||
echo "Not a terminal. Use the non-interactive form, e.g.:" >&2
|
||||
echo " $0 --all --sources=default-habbo-com" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "=== Catalog sync ==="
|
||||
echo
|
||||
|
||||
# ── Step 1: which sites to compare ───────────────────────────────────────────
|
||||
# Ask the CLI for the configured sources so the menu always matches the live
|
||||
# `clone_sources` setting (and its defaults) instead of a hardcoded list.
|
||||
mapfile -t SOURCE_ROWS < <("${RUN[@]}" --list-sources --tsv)
|
||||
if [ "${#SOURCE_ROWS[@]}" -eq 0 ]; then
|
||||
echo "No clone sources are configured." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
IDS=()
|
||||
NAMES=()
|
||||
echo "Compare this catalog against:"
|
||||
for i in "${!SOURCE_ROWS[@]}"; do
|
||||
IFS=$'\t' read -r SOURCE_ID SOURCE_NAME _SOURCE_URL <<<"${SOURCE_ROWS[$i]}"
|
||||
IDS+=("$SOURCE_ID")
|
||||
NAMES+=("$SOURCE_NAME")
|
||||
printf " %2d) %-24s %s\n" "$((i + 1))" "$SOURCE_NAME" "$SOURCE_ID"
|
||||
done
|
||||
echo
|
||||
echo " a) all sources above"
|
||||
echo " Enter) preset: ${NAMES[0]}"
|
||||
|
||||
read -r -p "Sources [a, 1-${#IDS[@]}, Enter]: " SOURCE_CHOICE
|
||||
|
||||
SELECTED_FLAG=""
|
||||
case "$SOURCE_CHOICE" in
|
||||
"" | "Habbo" | "habbo" | "habbo.com")
|
||||
# Habbo is the first default preset (Habbo (COM)).
|
||||
SELECTED_FLAG="--sources=${IDS[0]}"
|
||||
SOURCE_LABEL="${NAMES[0]}"
|
||||
;;
|
||||
a | A | all)
|
||||
SOURCE_LABEL="all sources"
|
||||
;;
|
||||
*)
|
||||
SELECTED_IDS=()
|
||||
IFS=', ' read -r -a CHOICES <<<"$SOURCE_CHOICE"
|
||||
for CHOICE in "${CHOICES[@]}"; do
|
||||
[ -z "$CHOICE" ] && continue
|
||||
case "$CHOICE" in
|
||||
*[!0-9]* | "")
|
||||
echo "Invalid choice: '$CHOICE'" >&2
|
||||
exit 1
|
||||
;;
|
||||
*)
|
||||
if [ "$CHOICE" -lt 1 ] || [ "$CHOICE" -gt "${#IDS[@]}" ]; then
|
||||
echo "Out of range: $CHOICE" >&2
|
||||
exit 1
|
||||
fi
|
||||
SELECTED_IDS+=("${IDS[$((CHOICE - 1))]}")
|
||||
;;
|
||||
esac
|
||||
done
|
||||
if [ "${#SELECTED_IDS[@]}" -eq 0 ]; then
|
||||
echo "No sources selected." >&2
|
||||
exit 1
|
||||
fi
|
||||
SELECTED_FLAG="--sources=$(IFS=,; echo "${SELECTED_IDS[*]}")"
|
||||
SOURCE_LABEL="${#SELECTED_IDS[@]} source(s)"
|
||||
;;
|
||||
esac
|
||||
|
||||
# ── Step 2: what may be changed ──────────────────────────────────────────────
|
||||
cat <<EOF
|
||||
|
||||
What should be done? (comparing with: $SOURCE_LABEL)
|
||||
1) Audit only — nothing is written
|
||||
2) Repair furniture icons + nitro bundles
|
||||
3) 2 + FurnitureData.json + catalog structure
|
||||
4) 3 + generate the catalog SQL (saved, not applied)
|
||||
5) 4 + APPLY the catalog SQL to the catalog
|
||||
6) 5 + import the furniture the source has and this hotel lacks
|
||||
(downloads icon + bundle, adds FurnitureData.json, creates the catalog
|
||||
page and the offer for every new item)
|
||||
7) 6 + reconcile FurnitureData.json / sprite ids / offer ids
|
||||
8) 7 + align items_base ids and rename legacy .nitro bundles (heaviest)
|
||||
d) Dry run — only report what 6 would import
|
||||
q) Cancel
|
||||
EOF
|
||||
|
||||
read -r -p "Action [1]: " ACTION_CHOICE
|
||||
ACTION_CHOICE="${ACTION_CHOICE:-1}"
|
||||
|
||||
ACTIONS=(audit)
|
||||
CONFIRM=""
|
||||
# Empty for the audit-only branch on purpose: no repair flags are passed.
|
||||
ACTION_FLAGS=()
|
||||
# Prints a warning when the chosen source only serves furnidata (no .nitro
|
||||
# bundles), so the import step would find nothing to download.
|
||||
warn_compare_only_sources() {
|
||||
case "$SOURCE_LABEL" in
|
||||
"Habbo (COM)" | "Habbo (NL)" | "Habbo (DE)" | "Habbo (FR)" | \
|
||||
"Habbo (ES)" | "Habbo (FI)" | "Habbo (BR)" | "Habbo (TR)" | \
|
||||
"Habbo (IT - GitHub mirror)" | "YabboHotel" | "Wibbo" | "Hubba.cc")
|
||||
echo
|
||||
echo "Note: ${SOURCE_LABEL} only serves furnidata — there are no .nitro"
|
||||
echo "bundles to import, so the import step can only COMPARE."
|
||||
echo "To actually import furniture, pick a source that serves bundles"
|
||||
echo "(Habblet City, VirtualCity, SodaStudios, Habboon, Leet, Hubbly) or 'a'."
|
||||
;;
|
||||
esac
|
||||
}
|
||||
case "$ACTION_CHOICE" in
|
||||
1)
|
||||
ACTIONS=(audit "[nothing written]")
|
||||
;;
|
||||
2)
|
||||
ACTIONS=(audit "icons + nitro bundles downloaded")
|
||||
ACTION_FLAGS=(--icons --nitros)
|
||||
;;
|
||||
3)
|
||||
ACTIONS=(audit "icons + nitro bundles + FurnitureData.json + structure")
|
||||
ACTION_FLAGS=(--all)
|
||||
;;
|
||||
4)
|
||||
ACTIONS=(audit "everything + catalog SQL generated")
|
||||
ACTION_FLAGS=(--all --sql)
|
||||
;;
|
||||
5)
|
||||
ACTIONS=(audit "everything + catalog SQL APPLIED")
|
||||
ACTION_FLAGS=(--all --sql --apply-sql)
|
||||
CONFIRM="This writes to the catalog. Continue? [y/N]: "
|
||||
;;
|
||||
6)
|
||||
ACTIONS=(audit "everything + catalog SQL + import new furniture")
|
||||
ACTION_FLAGS=(--all --sql --apply-sql --import-new)
|
||||
CONFIRM="This writes to the catalog AND imports every missing item. Continue? [y/N]: "
|
||||
;;
|
||||
7)
|
||||
ACTIONS=(audit "everything + import + reconcile")
|
||||
ACTION_FLAGS=(
|
||||
--all --sql --apply-sql --import-new --reconcile --fix-sprite-ids
|
||||
--rebuild-offers
|
||||
)
|
||||
CONFIRM="This writes to the catalog AND imports every missing item. Continue? [y/N]: "
|
||||
;;
|
||||
8)
|
||||
ACTIONS=(audit "everything + import + reconcile + id alignment + rename")
|
||||
ACTION_FLAGS=(
|
||||
--all --sql --apply-sql --import-new --reconcile --fix-sprite-ids
|
||||
--rebuild-offers --align-ids --rename-bundles --apply
|
||||
)
|
||||
read -r -p " This rewrites items_base ids and renames bundle files. Type RUN to confirm: " REPLY
|
||||
if [ "$REPLY" != "RUN" ]; then
|
||||
echo "Cancelled."
|
||||
exit 0
|
||||
fi
|
||||
;;
|
||||
d | D)
|
||||
ACTIONS=(audit "report only — nothing is imported")
|
||||
ACTION_FLAGS=(--import-new --dry-run)
|
||||
;;
|
||||
q | Q)
|
||||
echo "Cancelled."
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "Invalid action: '$ACTION_CHOICE'" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
echo
|
||||
printf 'Running: %s against %s\n' "${ACTIONS[1]}" "$SOURCE_LABEL"
|
||||
case "$ACTION_FLAGS" in
|
||||
*--import-new*) warn_compare_only_sources ;;
|
||||
esac
|
||||
if [ -n "$CONFIRM" ]; then
|
||||
read -r -p "$CONFIRM" REPLY
|
||||
if [ "$REPLY" != "y" ] && [ "$REPLY" != "Y" ]; then
|
||||
echo "Cancelled."
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
echo
|
||||
# shellcheck disable=SC2086
|
||||
"${RUN[@]}" $SELECTED_FLAG "${ACTION_FLAGS[@]}"
|
||||
@@ -0,0 +1,259 @@
|
||||
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
canImportBundles,
|
||||
DEFAULT_CATALOG_SYNC_FLAGS,
|
||||
describeActions,
|
||||
parseCatalogSyncArgs,
|
||||
selectImportCandidates,
|
||||
writeGeneratedSql,
|
||||
} from "./catalog-sync";
|
||||
|
||||
const entry = (classname: string) =>
|
||||
({ classname, name: classname, itemType: "s" }) as never;
|
||||
|
||||
describe("catalog-sync argument parsing", () => {
|
||||
it("defaults to a read-only audit with no filters", () => {
|
||||
expect(parseCatalogSyncArgs([])).toEqual({
|
||||
...DEFAULT_CATALOG_SYNC_FLAGS,
|
||||
sourceIds: [],
|
||||
});
|
||||
expect(describeActions(parseCatalogSyncArgs([]))).toBe("audit");
|
||||
});
|
||||
|
||||
it("accepts comma-separated source ids", () => {
|
||||
const flags = parseCatalogSyncArgs([
|
||||
"--sources=default-habbo-com,default-wibbo",
|
||||
]);
|
||||
expect(flags.sourceIds).toEqual(["default-habbo-com", "default-wibbo"]);
|
||||
});
|
||||
|
||||
it("accepts display names, including ones with spaces", () => {
|
||||
const flags = parseCatalogSyncArgs(["--sources=Habbo (COM), Wibbo"]);
|
||||
expect(flags.sourceIds).toEqual(["Habbo (COM)", "Wibbo"]);
|
||||
});
|
||||
|
||||
it("trims and drops empty source entries", () => {
|
||||
expect(parseCatalogSyncArgs(["--sources= a , ,b,"]).sourceIds).toEqual([
|
||||
"a",
|
||||
"b",
|
||||
]);
|
||||
});
|
||||
|
||||
it("turns --all into every repair step", () => {
|
||||
const flags = parseCatalogSyncArgs(["--all"]);
|
||||
expect(flags.repairIcons).toBe(true);
|
||||
expect(flags.repairNitros).toBe(true);
|
||||
expect(flags.repairFurniData).toBe(true);
|
||||
expect(flags.repairStructure).toBe(true);
|
||||
expect(flags.checkFurniDataIds).toBe(true);
|
||||
// Generating/applying SQL is never implied by a repair.
|
||||
expect(flags.generateSql).toBe(false);
|
||||
expect(flags.applySql).toBe(false);
|
||||
});
|
||||
|
||||
it("treats applying SQL as implying generation", () => {
|
||||
expect(parseCatalogSyncArgs(["--apply-sql"]).generateSql).toBe(true);
|
||||
expect(parseCatalogSyncArgs(["--organize-sql"]).generateSql).toBe(true);
|
||||
expect(describeActions(parseCatalogSyncArgs(["--apply-sql"]))).toBe(
|
||||
"audit + apply-sql",
|
||||
);
|
||||
});
|
||||
|
||||
it("accepts the alias spellings of the repair flags", () => {
|
||||
const flags = parseCatalogSyncArgs([
|
||||
"--repair-icons",
|
||||
"--repair-nitros",
|
||||
"--repair-furnidata",
|
||||
"--repair-structure",
|
||||
]);
|
||||
expect(flags.repairIcons).toBe(true);
|
||||
expect(flags.repairNitros).toBe(true);
|
||||
expect(flags.repairFurniData).toBe(true);
|
||||
expect(flags.repairStructure).toBe(true);
|
||||
});
|
||||
|
||||
it("ignores a bare -- separator", () => {
|
||||
expect(parseCatalogSyncArgs(["--", "--icons"]).repairIcons).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects an unknown flag", () => {
|
||||
expect(() => parseCatalogSyncArgs(["--nope"])).toThrow(/Unknown flag/);
|
||||
});
|
||||
|
||||
it("rejects --sources without a value", () => {
|
||||
expect(() => parseCatalogSyncArgs(["--sources"])).toThrow(/needs a value/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("catalog-sync import selection", () => {
|
||||
it("skips furniture this hotel already has", () => {
|
||||
const candidates = selectImportCandidates(
|
||||
[entry("chair"), entry("table"), entry("") as never],
|
||||
new Set(["chair"]),
|
||||
);
|
||||
expect(candidates.map((c) => c.classname)).toEqual(["table"]);
|
||||
});
|
||||
|
||||
it("is case-insensitive against the present set", () => {
|
||||
const candidates = selectImportCandidates(
|
||||
[entry("Chair"), entry("table")],
|
||||
new Set(["chair"]),
|
||||
);
|
||||
expect(candidates.map((c) => c.classname)).toEqual(["table"]);
|
||||
});
|
||||
|
||||
it("honours the --only filter, also case-insensitively", () => {
|
||||
const candidates = selectImportCandidates(
|
||||
[entry("Chair"), entry("table"), entry("lamp")],
|
||||
new Set(),
|
||||
{ only: ["chair"] },
|
||||
);
|
||||
expect(candidates.map((c) => c.classname)).toEqual(["Chair"]);
|
||||
});
|
||||
|
||||
it("caps the list with --limit", () => {
|
||||
const candidates = selectImportCandidates(
|
||||
[entry("a"), entry("b"), entry("c")],
|
||||
new Set(),
|
||||
{ limit: 2 },
|
||||
);
|
||||
expect(candidates.map((c) => c.classname)).toEqual(["a", "b"]);
|
||||
});
|
||||
|
||||
it("returns everything when no limit is given", () => {
|
||||
expect(
|
||||
selectImportCandidates([entry("a"), entry("b")], new Set(), {
|
||||
limit: 0,
|
||||
}).length,
|
||||
).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("catalog-sync import feasibility", () => {
|
||||
it("imports from a source that serves bundles", () => {
|
||||
expect(
|
||||
canImportBundles({
|
||||
id: "default-habblet-city",
|
||||
name: "Habblet City",
|
||||
furnidataUrl: "https://www.habblet.city/gamedata/furnidata.json",
|
||||
nitroBaseUrl: "https://www.habblet.city/nitro/bundled/furniture",
|
||||
iconBaseUrl: "",
|
||||
hotel: "com",
|
||||
} as never),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
// The Habbo presets have no nitro/icon base URL: their furnidata can be
|
||||
// compared, but there is nothing to download from.
|
||||
it("does not import from a compare-only source", () => {
|
||||
expect(
|
||||
canImportBundles({
|
||||
id: "default-habbo-com",
|
||||
name: "Habbo (COM)",
|
||||
furnidataUrl: "https://www.habbo.com/gamedata/furnidata_json/1",
|
||||
nitroBaseUrl: "",
|
||||
iconBaseUrl: "",
|
||||
hotel: "com",
|
||||
} as never),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("catalog-sync step composition", () => {
|
||||
it("--everything turns on every step except the writing ones", () => {
|
||||
const flags = parseCatalogSyncArgs(["--everything"]);
|
||||
expect(describeActions(flags)).toBe(
|
||||
[
|
||||
"audit",
|
||||
"icons",
|
||||
"nitros",
|
||||
"furnidata",
|
||||
"structure",
|
||||
"id-check",
|
||||
"import",
|
||||
"reconcile",
|
||||
"sprite-ids",
|
||||
"offers",
|
||||
].join(" + "),
|
||||
);
|
||||
// Destructive steps stay opt-in even inside --everything.
|
||||
expect(flags.applySql).toBe(false);
|
||||
expect(flags.apply).toBe(false);
|
||||
expect(flags.alignIds).toBe(false);
|
||||
expect(flags.renameBundles).toBe(false);
|
||||
});
|
||||
|
||||
it("marks an import as a dry run", () => {
|
||||
expect(
|
||||
describeActions(parseCatalogSyncArgs(["--import-new", "--dry-run"])),
|
||||
).toContain("import (dry)");
|
||||
});
|
||||
|
||||
it("marks the child steps as reporting unless --apply is given", () => {
|
||||
expect(
|
||||
describeActions(
|
||||
parseCatalogSyncArgs(["--align-ids", "--rename-bundles"]),
|
||||
),
|
||||
).toContain("align-ids");
|
||||
expect(
|
||||
describeActions(
|
||||
parseCatalogSyncArgs(["--align-ids", "--rename-bundles", "--apply"]),
|
||||
),
|
||||
).toContain("rename=apply-bundles");
|
||||
});
|
||||
|
||||
it("parses the import tuning flags", () => {
|
||||
const flags = parseCatalogSyncArgs([
|
||||
"--import-new",
|
||||
"--limit=50",
|
||||
"--concurrency=8",
|
||||
"--only=chair,table",
|
||||
]);
|
||||
expect(flags.importLimit).toBe(50);
|
||||
expect(flags.importConcurrency).toBe(8);
|
||||
expect(flags.importOnly).toEqual(["chair", "table"]);
|
||||
});
|
||||
|
||||
it("rejects a non-numeric --limit", () => {
|
||||
expect(() => parseCatalogSyncArgs(["--limit=abc"])).toThrow(/integer/);
|
||||
});
|
||||
|
||||
it("rejects --dry-run without an import to preview", () => {
|
||||
expect(() => parseCatalogSyncArgs(["--dry-run"])).toThrow(
|
||||
/together with --import-new/,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("generated catalog SQL output", () => {
|
||||
let cwd = "";
|
||||
|
||||
beforeEach(() => {
|
||||
cwd = process.cwd();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
process.chdir(cwd);
|
||||
});
|
||||
|
||||
it("writes the SQL under storage/catalog-sync with a trailing newline", async () => {
|
||||
let target = "";
|
||||
try {
|
||||
const dir = mkdtempSync(join(tmpdir(), "catalog-sync-sql-"));
|
||||
target = dir;
|
||||
process.chdir(dir);
|
||||
const file = await writeGeneratedSql("INSERT INTO `catalog_items` (1);");
|
||||
expect(file.startsWith("storage/catalog-sync/")).toBe(true);
|
||||
expect(file.endsWith(".sql")).toBe(true);
|
||||
expect(readFileSync(join(dir, file), "utf8")).toBe(
|
||||
"INSERT INTO `catalog_items` (1);\n",
|
||||
);
|
||||
} finally {
|
||||
if (target) rmSync(target, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,815 @@
|
||||
// Command-line driver for the catalog audit/repair pipeline.
|
||||
//
|
||||
// The same engine the admin "Catalog audit" UI streams from (`runCatalogAudit`),
|
||||
// for the terminal: audit the hotel catalog against one or more clone sources
|
||||
// (Habbo and the retros), fix the furniture that is broken, and bring missing
|
||||
// items into the catalog — optionally writing and applying the catalog SQL.
|
||||
//
|
||||
// Usage:
|
||||
// pnpm catalog:sync # interactive menu (scripts/catalog-sync.sh)
|
||||
// pnpm exec tsx scripts/catalog-sync.ts --list-sources
|
||||
// pnpm exec tsx scripts/catalog-sync.ts --sources="Habbo (COM)" # audit only
|
||||
// pnpm exec tsx scripts/catalog-sync.ts --all --sources=default-habbo-com
|
||||
// pnpm exec tsx scripts/catalog-sync.ts --all --sql --apply-sql # + write/apply SQL
|
||||
//
|
||||
// Flags:
|
||||
// --list-sources print the configured clone sources and exit
|
||||
// --tsv with the above, print `id\tname\tfurnidataUrl` rows
|
||||
// --sources=<a,b> compare against these sources (id or name, comma-separated)
|
||||
// --icons re-download missing furniture icons from the sources
|
||||
// --nitros re-download missing .nitro / swf bundles (stored as .hab)
|
||||
// --furnidata repair FurnitureData.json (add missing, drop duplicates)
|
||||
// --structure repair orphaned / duplicate catalog rows
|
||||
// --check-ids check FurnitureData.json for spriteId conflicts
|
||||
// --sql generate the catalog SQL for items missing an offer
|
||||
// --apply-sql apply the generated SQL (implies --sql)
|
||||
// --organize-sql order the generated SQL by catalog page (implies --sql)
|
||||
// --all --icons --nitros --furnidata --structure --check-ids
|
||||
// --import-new import furniture the source has and this hotel lacks
|
||||
// --only=<a,b> restrict the import to these classnames
|
||||
// --limit=N stop the import after N items
|
||||
// --concurrency=N parallel imports (default 3)
|
||||
// --reconcile align FurnitureData.json with items_base after importing
|
||||
// --fix-sprite-ids correct items_base.sprite_id drift
|
||||
// --rebuild-offers rebuild catalog offer_id mirrors
|
||||
// --align-ids move items_base ids onto their furnidata ids (report;
|
||||
// add --apply to let the script execute it)
|
||||
// --rename-bundles rename legacy .nitro bundles to .hab (report; --apply runs it)
|
||||
// --json print one machine-readable JSON report
|
||||
// --dry-run with --import-new: list what would be imported, import nothing
|
||||
// --fail-on-errors exit 3 when error-severity issues remain
|
||||
//
|
||||
// Audit is read-only by default: nothing is written until one of the repair
|
||||
// flags is passed. One nuance: generating the catalog SQL also creates the
|
||||
// `catalog_pages` category sub-pages it references (exactly like the app's own
|
||||
// import does) — only the `catalog_items` rows wait for `--apply-sql`. Use
|
||||
// `--organize-sql` to also emit those page INSERTs into the .sql file.
|
||||
|
||||
import "./load-env";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { LOCAL_BUNDLE_EXT } from "@/lib/furni/classname";
|
||||
import type { AuditEvent, AuditSummary } from "@/lib/services/catalog-audit";
|
||||
import type { SourceFurni } from "@/lib/services/clone-import";
|
||||
import type { CloneSource } from "@/lib/services/clone-sources";
|
||||
|
||||
/** Where generated catalog SQL is written (`storage/` is gitignored). */
|
||||
function sqlOutputDir(): string {
|
||||
return join(process.cwd(), "storage", "catalog-sync");
|
||||
}
|
||||
|
||||
export type SourceRef = CloneSource;
|
||||
|
||||
export type AuditIssue = NonNullable<AuditEvent["issues"]>[number];
|
||||
export type MissingFromSource = NonNullable<
|
||||
AuditEvent["missingFromSources"]
|
||||
>[number];
|
||||
|
||||
export interface AuditReport {
|
||||
sources: SourceRef[];
|
||||
summary?: AuditSummary;
|
||||
issues: AuditIssue[];
|
||||
missingFromSources: MissingFromSource[];
|
||||
sqlFile?: string;
|
||||
imported?: ImportOutcome;
|
||||
}
|
||||
|
||||
export interface ImportOutcome {
|
||||
source: string;
|
||||
candidates: number;
|
||||
imported: number;
|
||||
skipped: number;
|
||||
failed: number;
|
||||
failures: Array<{ classname: string; error: string }>;
|
||||
/** Sources that were skipped because they cannot serve bundles. */
|
||||
skippedSources: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a source can actually be imported from.
|
||||
*
|
||||
* The clone import downloads `${nitroBaseUrl}/${classname}.nitro` and gives up
|
||||
* when that is not reachable. Habbo's own entries carry no nitro/icon base URLs
|
||||
* (their furnidata is served, their assets are not), so they are compare-only:
|
||||
* perfect for finding out what this hotel lacks, useless for actually fetching
|
||||
* it. The SWF-based path that works against `images.habbo.com` lives in
|
||||
* `furni-import.ts`, not in the clone importer this CLI uses.
|
||||
*/
|
||||
export function canImportBundles(source: SourceRef): boolean {
|
||||
return Boolean(source.nitroBaseUrl?.trim());
|
||||
}
|
||||
|
||||
export interface CatalogSyncFlags {
|
||||
listSources: boolean;
|
||||
tsv: boolean;
|
||||
sourceIds: string[];
|
||||
repairIcons: boolean;
|
||||
repairNitros: boolean;
|
||||
repairFurniData: boolean;
|
||||
repairStructure: boolean;
|
||||
checkFurniDataIds: boolean;
|
||||
generateSql: boolean;
|
||||
applySql: boolean;
|
||||
organizeSql: boolean;
|
||||
json: boolean;
|
||||
failOnErrors: boolean;
|
||||
importNew: boolean;
|
||||
dryRun: boolean;
|
||||
importLimit: number;
|
||||
importConcurrency: number;
|
||||
importOnly: string[];
|
||||
reconcile: boolean;
|
||||
fixSpriteIds: boolean;
|
||||
rebuildOffers: boolean;
|
||||
alignIds: boolean;
|
||||
renameBundles: boolean;
|
||||
apply: boolean;
|
||||
}
|
||||
|
||||
export const DEFAULT_CATALOG_SYNC_FLAGS: Omit<CatalogSyncFlags, "sourceIds"> = {
|
||||
listSources: false,
|
||||
tsv: false,
|
||||
repairIcons: false,
|
||||
repairNitros: false,
|
||||
repairFurniData: false,
|
||||
repairStructure: false,
|
||||
checkFurniDataIds: false,
|
||||
generateSql: false,
|
||||
applySql: false,
|
||||
organizeSql: false,
|
||||
json: false,
|
||||
failOnErrors: false,
|
||||
importNew: false,
|
||||
dryRun: false,
|
||||
importLimit: 0,
|
||||
importConcurrency: 3,
|
||||
importOnly: [],
|
||||
reconcile: false,
|
||||
fixSpriteIds: false,
|
||||
rebuildOffers: false,
|
||||
alignIds: false,
|
||||
renameBundles: false,
|
||||
apply: false,
|
||||
};
|
||||
|
||||
const BOOLEAN_FLAGS: Record<string, keyof CatalogSyncFlags> = {
|
||||
"--list-sources": "listSources",
|
||||
"--tsv": "tsv",
|
||||
"--icons": "repairIcons",
|
||||
"--repair-icons": "repairIcons",
|
||||
"--nitros": "repairNitros",
|
||||
"--repair-nitros": "repairNitros",
|
||||
"--furnidata": "repairFurniData",
|
||||
"--repair-furnidata": "repairFurniData",
|
||||
"--structure": "repairStructure",
|
||||
"--repair-structure": "repairStructure",
|
||||
"--check-ids": "checkFurniDataIds",
|
||||
"--sql": "generateSql",
|
||||
"--apply-sql": "applySql",
|
||||
"--organize-sql": "organizeSql",
|
||||
"--json": "json",
|
||||
"--fail-on-errors": "failOnErrors",
|
||||
"--import-new": "importNew",
|
||||
"--dry-run": "dryRun",
|
||||
"--reconcile": "reconcile",
|
||||
"--fix-sprite-ids": "fixSpriteIds",
|
||||
"--rebuild-offers": "rebuildOffers",
|
||||
"--align-ids": "alignIds",
|
||||
"--rename-bundles": "renameBundles",
|
||||
// Authorises the two heavyweight child steps that rewrite stored data.
|
||||
"--apply": "apply",
|
||||
};
|
||||
|
||||
const NUMBER_FLAGS: Record<string, keyof CatalogSyncFlags> = {
|
||||
"--limit": "importLimit",
|
||||
"--concurrency": "importConcurrency",
|
||||
};
|
||||
|
||||
const LIST_FLAGS: Record<string, keyof CatalogSyncFlags> = {
|
||||
"--only": "importOnly",
|
||||
};
|
||||
|
||||
export function parseCatalogSyncArgs(raw: string[]): CatalogSyncFlags {
|
||||
const flags: CatalogSyncFlags = {
|
||||
...DEFAULT_CATALOG_SYNC_FLAGS,
|
||||
sourceIds: [],
|
||||
};
|
||||
|
||||
for (const arg of raw) {
|
||||
if (arg === "--") continue;
|
||||
|
||||
if (arg === "--all") {
|
||||
flags.repairIcons = true;
|
||||
flags.repairNitros = true;
|
||||
flags.repairFurniData = true;
|
||||
flags.repairStructure = true;
|
||||
flags.checkFurniDataIds = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg === "--everything") {
|
||||
flags.repairIcons = true;
|
||||
flags.repairNitros = true;
|
||||
flags.repairFurniData = true;
|
||||
flags.repairStructure = true;
|
||||
flags.checkFurniDataIds = true;
|
||||
flags.importNew = true;
|
||||
flags.reconcile = true;
|
||||
flags.fixSpriteIds = true;
|
||||
flags.rebuildOffers = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
const separator = arg.indexOf("=");
|
||||
const name = separator >= 0 ? arg.slice(0, separator) : arg;
|
||||
|
||||
const booleanFlag = BOOLEAN_FLAGS[name];
|
||||
if (booleanFlag) {
|
||||
flags[booleanFlag] = true as never;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (name === "--sources" || name === "--source") {
|
||||
if (separator < 0)
|
||||
throw new Error(
|
||||
`${name} needs a value, e.g. --sources=default-habbo-com`,
|
||||
);
|
||||
const value = arg.slice(separator + 1);
|
||||
for (const part of value
|
||||
.split(",")
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean))
|
||||
flags.sourceIds.push(part);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (NUMBER_FLAGS[name]) {
|
||||
if (separator < 0)
|
||||
throw new Error(`${name} needs a value, e.g. ${name}=10`);
|
||||
const parsed = Number(arg.slice(separator + 1));
|
||||
if (!Number.isInteger(parsed) || parsed < 0)
|
||||
throw new Error(`${name} needs a non-negative integer`);
|
||||
flags[NUMBER_FLAGS[name]] = parsed as never;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (LIST_FLAGS[name]) {
|
||||
if (separator < 0)
|
||||
throw new Error(`${name} needs a value, e.g. ${name}=chair,table`);
|
||||
const value = arg.slice(separator + 1);
|
||||
for (const part of value
|
||||
.split(",")
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean))
|
||||
flags.importOnly.push(part);
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Unknown flag: ${arg} (run with --help for the supported flags)`,
|
||||
);
|
||||
}
|
||||
|
||||
// Applying SQL implies generating it, and organising it implies generating.
|
||||
if (flags.applySql) flags.generateSql = true;
|
||||
if (flags.organizeSql) flags.generateSql = true;
|
||||
|
||||
// A dry run without an import step has nothing to preview.
|
||||
if (flags.dryRun && !flags.importNew)
|
||||
throw new Error("--dry-run only makes sense together with --import-new");
|
||||
|
||||
if (flags.importNew && flags.applySql) {
|
||||
// Both write; that is allowed but it is worth making the operator think.
|
||||
logNote?.("--import-new and --apply-sql will both write to the catalog");
|
||||
}
|
||||
return flags;
|
||||
}
|
||||
|
||||
let logNote: ((line: string) => void) | undefined;
|
||||
|
||||
/**
|
||||
* Pick the furniture to import from a source's furnidata.
|
||||
*
|
||||
* Pure so the filtering rules (skip what we already have, `--only` filter,
|
||||
* `--limit` cap) can be tested without a database.
|
||||
*/
|
||||
export function selectImportCandidates(
|
||||
entries: SourceFurni[],
|
||||
present: Set<string>,
|
||||
options: { only?: string[]; limit?: number } = {},
|
||||
): SourceFurni[] {
|
||||
const normalize = (value: string) => value.trim().toLowerCase();
|
||||
const only = options.only?.length
|
||||
? new Set(options.only.map(normalize))
|
||||
: null;
|
||||
|
||||
const candidates = entries.filter((entry) => {
|
||||
if (!entry.classname) return false;
|
||||
if (present.has(normalize(entry.classname))) return false;
|
||||
if (only && !only.has(normalize(entry.classname))) return false;
|
||||
return true;
|
||||
});
|
||||
|
||||
const limit = options.limit ?? 0;
|
||||
return limit > 0 ? candidates.slice(0, limit) : candidates;
|
||||
}
|
||||
|
||||
/** Human-readable list of what the run will do. */
|
||||
export function describeActions(flags: CatalogSyncFlags): string {
|
||||
const actions = ["audit"];
|
||||
if (flags.repairIcons) actions.push("icons");
|
||||
if (flags.repairNitros) actions.push("nitros");
|
||||
if (flags.repairFurniData) actions.push("furnidata");
|
||||
if (flags.repairStructure) actions.push("structure");
|
||||
if (flags.checkFurniDataIds) actions.push("id-check");
|
||||
if (flags.importNew) actions.push(flags.dryRun ? "import (dry)" : "import");
|
||||
if (flags.reconcile) actions.push("reconcile");
|
||||
if (flags.fixSpriteIds) actions.push("sprite-ids");
|
||||
if (flags.rebuildOffers) actions.push("offers");
|
||||
if (flags.alignIds) actions.push(`align-ids${flags.apply ? "=apply" : ""}`);
|
||||
if (flags.renameBundles)
|
||||
actions.push(`rename${flags.apply ? "=apply" : "-dry"}-bundles`);
|
||||
if (flags.applySql) actions.push("apply-sql");
|
||||
else if (flags.generateSql) actions.push("generate-sql");
|
||||
if (flags.organizeSql) actions.push("organised");
|
||||
return actions.join(" + ");
|
||||
}
|
||||
|
||||
/** True when the file is being executed, not imported by a test. */
|
||||
function isEntryPoint(): boolean {
|
||||
const entry = process.argv[1];
|
||||
return (
|
||||
Boolean(entry) && import.meta.url === pathToFileURL(String(entry)).href
|
||||
);
|
||||
}
|
||||
|
||||
async function loadSourceCatalogue(): Promise<SourceRef[]> {
|
||||
const { listSources } = await import("@/lib/services/clone-sources");
|
||||
const sources = await listSources();
|
||||
// Full source objects: the import step needs the icon/nitro base URLs too.
|
||||
return sources.filter((source) => source.furnidataUrl);
|
||||
}
|
||||
|
||||
async function commandListSources(flags: CatalogSyncFlags): Promise<number> {
|
||||
const sources = await loadSourceCatalogue();
|
||||
|
||||
if (flags.tsv) {
|
||||
for (const source of sources)
|
||||
console.log([source.id, source.name, source.furnidataUrl].join("\t"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
console.log(`Configured clone sources (${sources.length}):`);
|
||||
for (const source of sources)
|
||||
console.log(` ${source.id}\t${source.name}\t${source.furnidataUrl}`);
|
||||
console.log(
|
||||
"\nCompare with: --sources=<id> (comma-separated; display names work too)",
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Persist the generated SQL so it can be reviewed or replayed by hand. */
|
||||
export async function writeGeneratedSql(sql: string): Promise<string> {
|
||||
await mkdir(sqlOutputDir(), { recursive: true });
|
||||
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
|
||||
const file = join(sqlOutputDir(), `catalog-${stamp}.sql`);
|
||||
await writeFile(file, sql.endsWith("\n") ? sql : `${sql}\n`, "utf8");
|
||||
return file.replace(`${process.cwd()}/`, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Run one of the repo's existing standalone maintenance scripts as a child
|
||||
* process, with the same Node conditions this CLI needs.
|
||||
*/
|
||||
function runChildScript(
|
||||
script: string,
|
||||
args: string[],
|
||||
log: (line: string) => void,
|
||||
): number {
|
||||
const nodeArgs = [
|
||||
"--conditions=react-server",
|
||||
"--import",
|
||||
"tsx",
|
||||
script,
|
||||
...args,
|
||||
];
|
||||
log(`· running scripts/${script} ${args.join(" ")}`.trimEnd());
|
||||
const result = spawnSync(process.execPath, nodeArgs, {
|
||||
cwd: process.cwd(),
|
||||
stdio: "inherit",
|
||||
env: process.env,
|
||||
});
|
||||
if (result.error) {
|
||||
console.error(`! ${script} failed to start: ${result.error.message}`);
|
||||
return 1;
|
||||
}
|
||||
return result.status ?? 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import furniture that exists at the compared source but not in this hotel.
|
||||
*
|
||||
* `cloneSingleFurni` downloads the bundle + icon, stores the bundle as
|
||||
* `LOCAL_BUNDLE_EXT`, appends the furnidata entry and creates the catalog page
|
||||
* plus the offer itself — so an import is not usable for furniture you want to
|
||||
* keep out of the catalog.
|
||||
*/
|
||||
async function stepImportNew(
|
||||
flags: CatalogSyncFlags,
|
||||
sources: SourceRef[],
|
||||
log: (line: string) => void,
|
||||
): Promise<ImportOutcome | undefined> {
|
||||
const { getClonableClassnames, fetchSourceFurnidata } = await import(
|
||||
"@/lib/services/clone-import"
|
||||
);
|
||||
const { cloneSingleFurni } = await import("@/lib/services/clone-import");
|
||||
|
||||
let planned = 0;
|
||||
let imported = 0;
|
||||
let skipped = 0;
|
||||
let failed = 0;
|
||||
const failures: Array<{ classname: string; error: string }> = [];
|
||||
const deferredEntries: Array<{
|
||||
entry: Record<string, unknown>;
|
||||
itemType: string;
|
||||
}> = [];
|
||||
let lastSource = "";
|
||||
const skippedSources: string[] = [];
|
||||
|
||||
for (const source of sources) {
|
||||
if (!canImportBundles(source)) {
|
||||
log(
|
||||
`! ${source.name} serves no .nitro bundles — it can be compared against, but nothing can be imported from it`,
|
||||
);
|
||||
skippedSources.push(source.name);
|
||||
continue;
|
||||
}
|
||||
if (flags.importLimit > 0 && planned >= flags.importLimit) break;
|
||||
const remaining = flags.importLimit
|
||||
? flags.importLimit - planned
|
||||
: Number.POSITIVE_INFINITY;
|
||||
|
||||
log(`· listing furniture missing at ${source.name}…`);
|
||||
const clonable = new Set(await getClonableClassnames(source));
|
||||
const entries = await fetchSourceFurnidata(source.furnidataUrl);
|
||||
const candidates = selectImportCandidates(entries, clonable, {
|
||||
only: flags.importOnly,
|
||||
limit: Number.isFinite(remaining) ? remaining : 0,
|
||||
});
|
||||
if (candidates.length === 0) {
|
||||
log(` nothing new to import from ${source.name}`);
|
||||
continue;
|
||||
}
|
||||
lastSource = source.name;
|
||||
|
||||
if (flags.dryRun) {
|
||||
log(
|
||||
` ${candidates.length} would be imported from ${source.name}${flags.importOnly.length ? " (filtered by --only)" : ""}`,
|
||||
);
|
||||
for (const entry of candidates.slice(0, 20))
|
||||
log(` ${entry.classname} — ${entry.name}`);
|
||||
if (candidates.length > 20)
|
||||
log(` … and ${candidates.length - 20} more`);
|
||||
planned += candidates.length;
|
||||
continue;
|
||||
}
|
||||
|
||||
planned += candidates.length;
|
||||
const started = Date.now();
|
||||
let done = 0;
|
||||
let index = 0;
|
||||
|
||||
const worker = async () => {
|
||||
while (index < candidates.length) {
|
||||
const entry = candidates[index++];
|
||||
const result = await cloneSingleFurni({
|
||||
source,
|
||||
entry,
|
||||
deferFurniData: true,
|
||||
});
|
||||
done += 1;
|
||||
if (result.ok) imported += 1;
|
||||
else if (result.skipped) skipped += 1;
|
||||
else {
|
||||
failed += 1;
|
||||
failures.push({
|
||||
classname: entry.classname,
|
||||
error: result.error ?? "unknown error",
|
||||
});
|
||||
}
|
||||
if (result.furniDataEntry)
|
||||
deferredEntries.push(
|
||||
result.furniDataEntry as {
|
||||
entry: Record<string, unknown>;
|
||||
itemType: string;
|
||||
},
|
||||
);
|
||||
|
||||
if (done % 10 === 0 || done === candidates.length) {
|
||||
const rate = Math.max(done / ((Date.now() - started) / 1000), 1);
|
||||
const etaMin = Math.round((candidates.length - done) / rate / 60);
|
||||
log(
|
||||
` ${source.name}: ${done}/${candidates.length} ${rate.toFixed(1)}/s, ETA ~${etaMin}m`,
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const workers = Math.max(
|
||||
1,
|
||||
Math.min(flags.importConcurrency, candidates.length),
|
||||
);
|
||||
await Promise.all(Array.from({ length: workers }, worker));
|
||||
}
|
||||
|
||||
if (!flags.dryRun && deferredEntries.length > 0) {
|
||||
const { appendFurniEntriesBulk } = await import(
|
||||
"@/lib/services/furni-data"
|
||||
);
|
||||
await appendFurniEntriesBulk(deferredEntries);
|
||||
log(`· FurnitureData.json: +${deferredEntries.length} entries (one write)`);
|
||||
}
|
||||
|
||||
return {
|
||||
source: lastSource,
|
||||
candidates: planned,
|
||||
imported,
|
||||
skipped,
|
||||
failed,
|
||||
failures: failures.slice(0, 25),
|
||||
skippedSources,
|
||||
};
|
||||
}
|
||||
|
||||
/** Keep FurnitureData.json consistent with items_base after the import. */
|
||||
async function stepReconcile(log: (line: string) => void): Promise<void> {
|
||||
const { reconcileFurniDataWithItemsBase } = await import(
|
||||
"@/lib/services/furni-data"
|
||||
);
|
||||
const result = await reconcileFurniDataWithItemsBase();
|
||||
log(
|
||||
`· reconcile: ${result.fixedIds} sprite id(s), ${result.fixedOfferIds} offer id(s), ${result.normalizedIds} normalised, ${result.fixedConflicts} conflict(s) resolved, ${result.missing} still missing`,
|
||||
);
|
||||
}
|
||||
|
||||
/** items_base.sprite_id must equal its id wherever furnidata carries the item. */
|
||||
async function stepFixSpriteIds(log: (line: string) => void): Promise<void> {
|
||||
const { verifyAndFixSpriteIds } = await import("@/lib/services/furni-data");
|
||||
const result = await verifyAndFixSpriteIds();
|
||||
log(`· sprite ids: ${result.checked} checked, ${result.fixed} corrected`);
|
||||
}
|
||||
|
||||
/** offer_id mirrors the row id, never a literal -1. */
|
||||
async function stepRebuildOffers(log: (line: string) => void): Promise<void> {
|
||||
const { rebuildCatalogOfferIds } = await import("@/lib/services/furni-data");
|
||||
const result = await rebuildCatalogOfferIds();
|
||||
log(`· offer ids: ${JSON.stringify(result)}`);
|
||||
}
|
||||
|
||||
export async function commandRun(flags: CatalogSyncFlags): Promise<number> {
|
||||
const catalogue = await loadSourceCatalogue();
|
||||
|
||||
// Resolve the selection up-front so a typo fails before anything is written.
|
||||
const known = new Set<string>();
|
||||
for (const source of catalogue) {
|
||||
known.add(source.id);
|
||||
known.add(source.name);
|
||||
}
|
||||
const unknown = flags.sourceIds.filter((id) => !known.has(id));
|
||||
if (unknown.length > 0) {
|
||||
console.error(
|
||||
`Unknown source: ${unknown.join(", ")}\nRun --list-sources to see the configured sites.`,
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const selected = flags.sourceIds.length
|
||||
? catalogue.filter(
|
||||
(source) =>
|
||||
flags.sourceIds.includes(source.id) ||
|
||||
flags.sourceIds.includes(source.name),
|
||||
)
|
||||
: catalogue;
|
||||
|
||||
const { runCatalogAudit } = await import("@/lib/services/catalog-audit");
|
||||
|
||||
const report: AuditReport = {
|
||||
sources: selected,
|
||||
issues: [],
|
||||
missingFromSources: [],
|
||||
};
|
||||
const quiet = flags.json;
|
||||
const log = (line: string) => {
|
||||
if (!quiet) console.log(line);
|
||||
};
|
||||
const started = Date.now();
|
||||
let pendingSql: string | undefined;
|
||||
|
||||
log(
|
||||
`Catalog sync — ${selected.length} source(s), ${describeActions(flags)} — bundles stored as ${LOCAL_BUNDLE_EXT} (FURNI_BUNDLE_EXT)` +
|
||||
(flags.applySql ? " [SQL WILL BE APPLIED]" : ""),
|
||||
);
|
||||
|
||||
// 1) Move items_base ids onto their furnidata ids before anything else, so
|
||||
// every later step sees stable ids. The script only reports without
|
||||
// `--apply`.
|
||||
if (flags.alignIds) {
|
||||
runChildScript(
|
||||
"scripts/align-db-ids-with-furnidata.ts",
|
||||
flags.apply ? ["--apply"] : [],
|
||||
log,
|
||||
);
|
||||
}
|
||||
|
||||
// 2) Audit + repair of what is already here.
|
||||
|
||||
await runCatalogAudit(
|
||||
(event) => {
|
||||
switch (event.type) {
|
||||
case "progress":
|
||||
log(`· ${event.message ?? ""}`);
|
||||
break;
|
||||
case "items_loaded":
|
||||
log(`· items_base: ${event.total} rows`);
|
||||
break;
|
||||
case "catalog_loaded":
|
||||
log(`· catalog_items: ${event.total} rows`);
|
||||
break;
|
||||
case "sql_ready":
|
||||
log(`· catalog SQL generated: ${event.sqlCount ?? 0} statement(s)`);
|
||||
pendingSql = event.sql;
|
||||
break;
|
||||
case "sql_applied":
|
||||
log(
|
||||
`· catalog SQL applied: ${event.applied ?? 0} (failed ${event.sqlFailed ?? 0})`,
|
||||
);
|
||||
break;
|
||||
case "furnidata_repair_complete":
|
||||
log(
|
||||
`· FurnitureData.json: +${event.furniData?.added ?? 0} entries, ${event.furniData?.removedDuplicates ?? 0} duplicate(s) removed`,
|
||||
);
|
||||
break;
|
||||
case "structure_repair_complete":
|
||||
log(
|
||||
`· structure: ${event.structure?.orphanedRemoved ?? 0} orphaned removed, ${event.structure?.duplicatesMerged ?? 0} duplicate(s) merged, ${event.structure?.remappedReferences ?? 0} reference(s) remapped`,
|
||||
);
|
||||
break;
|
||||
case "repair_complete":
|
||||
log(
|
||||
`· asset repair: ${event.repair?.succeeded ?? 0} ok, ${event.repair?.failed ?? 0} failed, ${event.repair?.skipped ?? 0} skipped`,
|
||||
);
|
||||
break;
|
||||
case "batch_complete":
|
||||
report.issues = event.issues ?? [];
|
||||
report.missingFromSources = event.missingFromSources ?? [];
|
||||
report.summary = event.summary;
|
||||
break;
|
||||
case "error":
|
||||
console.error(`! ${event.message ?? "audit error"}`);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
},
|
||||
{
|
||||
sourceIds: flags.sourceIds,
|
||||
repair: flags.repairIcons,
|
||||
repairNitros: flags.repairNitros,
|
||||
sql: flags.generateSql,
|
||||
applySql: flags.applySql,
|
||||
organizeSql: flags.organizeSql,
|
||||
repairFurniData: flags.repairFurniData,
|
||||
repairStructure: flags.repairStructure,
|
||||
checkFurniDataIds: flags.checkFurniDataIds,
|
||||
},
|
||||
);
|
||||
|
||||
if (pendingSql?.trim()) report.sqlFile = await writeGeneratedSql(pendingSql);
|
||||
|
||||
// 3) Import what the source has and this hotel does not.
|
||||
if (flags.importNew) {
|
||||
if (selected.length === 0) log("· no source selected, nothing to import");
|
||||
else report.imported = await stepImportNew(flags, selected, log);
|
||||
}
|
||||
|
||||
// 4) Post-import consistency, then the legacy bundle rename.
|
||||
if (flags.reconcile) await stepReconcile(log);
|
||||
if (flags.fixSpriteIds) await stepFixSpriteIds(log);
|
||||
if (flags.rebuildOffers) await stepRebuildOffers(log);
|
||||
if (flags.renameBundles) {
|
||||
runChildScript(
|
||||
"scripts/migrate-nitro-to-hab.ts",
|
||||
flags.apply ? ["--yes"] : ["--dry-run"],
|
||||
log,
|
||||
);
|
||||
}
|
||||
|
||||
if (flags.json) {
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{ flags, elapsedMs: Date.now() - started, ...report },
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
printSummary(report, started);
|
||||
if (report.sqlFile) console.log(`Catalog SQL: ${report.sqlFile}`);
|
||||
}
|
||||
|
||||
const errors = report.issues.filter((issue) => issue.severity === "error");
|
||||
if (flags.failOnErrors && errors.length > 0) {
|
||||
if (!quiet) console.error(`${errors.length} error(s) remain unresolved.`);
|
||||
return 3;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function printSummary(report: AuditReport, started: number): void {
|
||||
const summary = report.summary;
|
||||
const seconds = ((Date.now() - started) / 1000).toFixed(1);
|
||||
|
||||
console.log(`\nDone in ${seconds}s`);
|
||||
if (!summary) {
|
||||
console.log("No summary was produced (the audit stopped early).");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(` items_base ${summary.totalItemsBase}`);
|
||||
console.log(` catalog_items ${summary.totalCatalogItems}`);
|
||||
console.log(` missing catalog entry ${summary.missingCatalog}`);
|
||||
console.log(` orphaned catalog rows ${summary.orphanedCatalog}`);
|
||||
console.log(` duplicate classnames ${summary.duplicates}`);
|
||||
console.log(
|
||||
` missing icons ${summary.missingIcon} (repaired ${summary.iconsRepaired}, still missing ${summary.iconsStillMissing})`,
|
||||
);
|
||||
console.log(
|
||||
` missing nitro bundles ${summary.missingNitro} (repaired ${summary.nitrosRepaired}, still missing ${summary.nitrosStillMissing})`,
|
||||
);
|
||||
console.log(
|
||||
` catalog SQL ${summary.catalogSqlCount} stmt, applied ${summary.catalogSqlApplied}`,
|
||||
);
|
||||
console.log(` furnidata added ${summary.furniDataAdded}`);
|
||||
console.log(
|
||||
` not restorable ${summary.unrepairableCount} (no icon/bundle on any compared source)`,
|
||||
);
|
||||
|
||||
if (report.imported) {
|
||||
const outcome = report.imported;
|
||||
console.log(
|
||||
` imported from source ${outcome.imported} new (${outcome.skipped} already present, ${outcome.failed} failed)`,
|
||||
);
|
||||
if (outcome.skippedSources.length > 0)
|
||||
console.log(
|
||||
` compared only ${outcome.skippedSources.join(", ")} (no .nitro bundles to import from)`,
|
||||
);
|
||||
for (const failure of outcome.failures)
|
||||
console.log(` ${failure.classname}: ${failure.error}`);
|
||||
}
|
||||
|
||||
if (report.missingFromSources.length > 0) {
|
||||
const bySource = new Map<string, number>();
|
||||
for (const entry of report.missingFromSources)
|
||||
bySource.set(entry.sourceName, (bySource.get(entry.sourceName) ?? 0) + 1);
|
||||
console.log(
|
||||
`\n on compared sources but not in this hotel: ${report.missingFromSources.length}`,
|
||||
);
|
||||
for (const [name, count] of bySource)
|
||||
console.log(` ${name.padEnd(24)} ${count}`);
|
||||
}
|
||||
|
||||
const errors = report.issues.filter((issue) => issue.severity === "error");
|
||||
if (errors.length > 0) {
|
||||
console.log(`\n unresolved errors: ${errors.length}`);
|
||||
for (const issue of errors.slice(0, 20))
|
||||
console.log(` ${issue.classname ?? issue.type}: ${issue.message}`);
|
||||
if (errors.length > 20) console.log(` … and ${errors.length - 20} more`);
|
||||
}
|
||||
}
|
||||
|
||||
async function main(): Promise<number> {
|
||||
const flags = parseCatalogSyncArgs(process.argv.slice(2));
|
||||
try {
|
||||
if (flags.listSources) return await commandListSources(flags);
|
||||
return await commandRun(flags);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Catalog sync failed: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (isEntryPoint()) {
|
||||
main().then(
|
||||
(code) => {
|
||||
process.exit(code);
|
||||
},
|
||||
(error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
# Create/update the Cloudflare Cache Rule that stores the CMS public API
|
||||
# allowlist plus the client-facing gamedata tree at the edge (the routes nginx
|
||||
# tags with `Cache-Tag: cms-public` respectievelijk `cms-gamedata`).
|
||||
# allowlist, the client-facing gamedata tree and the avatar/badge imaging
|
||||
# endpoints at the edge (the routes nginx tags with `Cache-Tag: cms-public`
|
||||
# respectievelijk `cms-gamedata`, en `/api/imaging/` dat zijn eigen s-maxage
|
||||
# stuurt).
|
||||
#
|
||||
# Why a rule is required: Cloudflare only caches a handful of file extensions
|
||||
# by default; `/api/*` responses are served `cf-cache-status: DYNAMIC` even
|
||||
@@ -35,12 +37,18 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ENV_FILE="$SCRIPT_DIR/../.env"
|
||||
BASE="https://api.cloudflare.com/client/v4"
|
||||
PHASE="http_request_cache_settings"
|
||||
DESCRIPTION="EpicNabbo CMS public API + gamedata edge cache (cms-public, cms-gamedata)"
|
||||
DESCRIPTION="EpicNabbo CMS public API + gamedata + imaging edge cache (cms-public, cms-gamedata, cms-imaging)"
|
||||
|
||||
# Cache de allowlist exact zoals nginx hem tagt (deployment/proxy/nginx-cms.conf).
|
||||
# Geen regex: `matches` vereist Business; vrije operators zijn `in` en
|
||||
# `starts_with()`.
|
||||
EXPRESSION='(http.request.method eq "GET") and (http.request.uri.path in { "/api/staff" "/api/teams" "/api/guilds" "/api/photos" "/api/leaderboard" "/api/online" "/api/online/count" "/api/shop" "/api/shop/categories" "/api/values" "/api/values/categories" "/api/radio/current-dj" "/api/radio/points/leaderboard" } or starts_with(http.request.uri.path, "/api/values/") or starts_with(http.request.uri.path, "/gamedata/"))'
|
||||
#
|
||||
# `/api/imaging/` (avatars, badges) staat erbij omdat de CMS-route zelf al
|
||||
# `s-maxage=86400` + `stale-while-revalidate` stuurt: de edge kan een avatar een
|
||||
# dag vasthouden, zodat herhaalde requests de lokale renderer nooit raken. De
|
||||
# volledige URL (inclusief figure/code) is de cache-key, dus geen vervuiling
|
||||
# tussen gebruikers.
|
||||
EXPRESSION='(http.request.method eq "GET") and (http.request.uri.path in { "/api/staff" "/api/teams" "/api/guilds" "/api/photos" "/api/leaderboard" "/api/online" "/api/online/count" "/api/shop" "/api/shop/categories" "/api/values" "/api/values/categories" "/api/radio/current-dj" "/api/radio/points/leaderboard" } or starts_with(http.request.uri.path, "/api/values/") or starts_with(http.request.uri.path, "/api/imaging/") or starts_with(http.request.uri.path, "/gamedata/"))'
|
||||
|
||||
load_env() {
|
||||
local name="$1"
|
||||
|
||||
@@ -45,11 +45,28 @@ for (const image of nodeImages) {
|
||||
);
|
||||
}
|
||||
|
||||
const cmpVersion = (a, b) => {
|
||||
const pa = a.split(".").map((part) => Number.parseInt(part, 10));
|
||||
const pb = b.split(".").map((part) => Number.parseInt(part, 10));
|
||||
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
||||
const diff = (pa[i] ?? 0) - (pb[i] ?? 0);
|
||||
if (diff !== 0) return diff;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
// `.nvmrc` is the recommended version for reproducible local development and
|
||||
// the exact Docker base image (both asserted above), but the *runtime* we run
|
||||
// on may be any version the package.json engines range accepts. Requiring an
|
||||
// exact patch match here would fail on every Node.js patch release, even when
|
||||
// the version is explicitly supported.
|
||||
if (!process.argv.includes("--static")) {
|
||||
assert.equal(
|
||||
process.versions.node,
|
||||
pinnedVersion,
|
||||
"the active Node.js runtime must match .nvmrc",
|
||||
const active = process.versions.node;
|
||||
const upperBound = `${major + 1}.0.0`;
|
||||
assert.ok(
|
||||
cmpVersion(active, pinnedVersion) >= 0 &&
|
||||
cmpVersion(active, upperBound) < 0,
|
||||
`the active Node.js runtime (${active}) must satisfy package.json engines.node (${packageJson.engines.node}); .nvmrc pins ${pinnedVersion} as the recommended version`,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Executable
+124
@@ -0,0 +1,124 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for the port-selection and port-conflict logic in scripts/ci-deploy.sh.
|
||||
#
|
||||
# Background: on a host where both blue/green slots answer /api/health, the
|
||||
# original read_active_port() counted healthy slots and only consulted the nginx
|
||||
# upstream when the count was not exactly 1. With two healthy slots it fell back
|
||||
# to the upstream file, but an operator `docker compose up` can leave an extra
|
||||
# replica behind, after which the fallback picked slot A regardless of which slot
|
||||
# was really live. The candidate then tried to start on an occupied port, and the
|
||||
# health probe answered from the pre-existing container on that port instead of
|
||||
# the candidate — producing 30 failed "expected release never became healthy"
|
||||
# attempts against a release that was never serving.
|
||||
#
|
||||
# The functions are extracted from ci-deploy.sh rather than copied so this test
|
||||
# cannot drift from the script it protects.
|
||||
set -Eeuo pipefail
|
||||
|
||||
deploy_script="$(dirname "$0")/ci-deploy.sh"
|
||||
[[ -r "$deploy_script" ]] || { echo "cannot read $deploy_script" >&2; exit 1; }
|
||||
|
||||
# Pull the two functions out of the real script.
|
||||
extract() {
|
||||
sed -n "/^$1() {/,/^}/p" "$deploy_script"
|
||||
}
|
||||
|
||||
read_active_port_fn="$(extract read_active_port)"
|
||||
assert_port_free_fn="$(extract assert_port_free)"
|
||||
answers_health_fn="$(extract answers_health)"
|
||||
|
||||
if [ -z "$read_active_port_fn" ] || [ -z "$assert_port_free_fn" ] || [ -z "$answers_health_fn" ]; then
|
||||
echo "could not extract functions from $deploy_script" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
slot_a_port=3002
|
||||
slot_b_port=3003
|
||||
|
||||
fail() { echo "FAIL: $*" >&2; exit 1; }
|
||||
|
||||
# ── read_active_port ──────────────────────────────────────────────────────────
|
||||
# $1 = upstream body ("none" for a missing file), $2..$3 = ports that answer.
|
||||
run_read_active_port() {
|
||||
local body="$1" a="$2" b="$3" tmp
|
||||
tmp="$(mktemp)"
|
||||
if [ "$body" = "none" ]; then
|
||||
tmp=/tmp/ci-deploy-test-nonexistent-upstream-$$
|
||||
rm -f "$tmp"
|
||||
else
|
||||
printf '%s\n' "$body" >"$tmp"
|
||||
fi
|
||||
CMS_UPSTREAM_FILE="$tmp" \
|
||||
PORT_A_HEALTHY="$a" PORT_B_HEALTHY="$b" \
|
||||
bash -c "
|
||||
slot_a_port=$slot_a_port
|
||||
slot_b_port=$slot_b_port
|
||||
upstream_file=\"\$CMS_UPSTREAM_FILE\"
|
||||
$read_active_port_fn
|
||||
# Defined after the extracted function on purpose: answers_health is a
|
||||
# collaborator here, and the test substitutes a deterministic stub for it.
|
||||
answers_health() {
|
||||
local p=\$1 want
|
||||
case \$p in
|
||||
$slot_a_port) want=\"\$PORT_A_HEALTHY\" ;;
|
||||
$slot_b_port) want=\"\$PORT_B_HEALTHY\" ;;
|
||||
*) want='' ;;
|
||||
esac
|
||||
[ \"\$want\" = yes ]
|
||||
}
|
||||
read_active_port
|
||||
echo
|
||||
" 2>/dev/null
|
||||
rm -f "$tmp"
|
||||
}
|
||||
|
||||
# nginx points at slot B and both answer -> trust the upstream file.
|
||||
got="$(run_read_active_port 'server 127.0.0.1:3003 max_fails=2;' yes yes)"
|
||||
[ "$got" = "$slot_b_port" ] || fail "nginx->3003 with both healthy: got '$got', want 3003"
|
||||
|
||||
got="$(run_read_active_port 'server 127.0.0.1:3002 max_fails=2;' yes yes)"
|
||||
[ "$got" = "$slot_a_port" ] || fail "nginx->3002 with both healthy: got '$got', want 3002"
|
||||
|
||||
# The regression: both healthy, nginx points at B, but slot A is an unrelated
|
||||
# leftover replica. The upstream file is the only thing that knows which slot is
|
||||
# live, so it must win.
|
||||
got="$(run_read_active_port 'server 127.0.0.1:3003 max_fails=2;' yes yes)"
|
||||
[ "$got" != "$slot_a_port" ] || fail "both healthy: fell back to slot A while nginx serves 3003"
|
||||
|
||||
# Upstream names a dead slot: fall back to a slot that actually answers, never to
|
||||
# the dead port itself.
|
||||
got="$(run_read_active_port 'server 127.0.0.1:3002 max_fails=2;' no yes)"
|
||||
[ "$got" = "$slot_b_port" ] || fail "nginx->3002 unhealthy, B healthy: got '$got', want 3003"
|
||||
|
||||
# Nothing answers at all: read_active_port still has to name a slot, otherwise the
|
||||
# rollback path has no target.
|
||||
got="$(run_read_active_port 'server 127.0.0.1:3003 max_fails=2;' no no)"
|
||||
[ "$got" = "$slot_b_port" ] || fail "nothing healthy: got '$got', want the upstream port 3003"
|
||||
|
||||
# No upstream file at all: pick a slot that answers.
|
||||
got="$(run_read_active_port none no yes)"
|
||||
[ "$got" = "$slot_b_port" ] || fail "no upstream, B healthy: got '$got', want 3003"
|
||||
|
||||
got="$(run_read_active_port none yes no)"
|
||||
[ "$got" = "$slot_a_port" ] || fail "no upstream, A healthy: got '$got', want 3002"
|
||||
|
||||
# ── assert_port_free ─────────────────────────────────────────────────────────
|
||||
# Runs against real loopback ports: 3999 is intentionally unused, so the check
|
||||
# must report it free.
|
||||
bash -c "
|
||||
$assert_port_free_fn
|
||||
assert_port_free 3999 candidate >/dev/null 2>&1
|
||||
" || fail "a port with no listener must be reported as free"
|
||||
|
||||
# On this host 3002 is held by a CMS container, so the check must fail. Skip when
|
||||
# it genuinely is free, otherwise the assertion would be meaningless.
|
||||
if ss -ltn 2>/dev/null | grep -qE '127\.0\.0\.1:3002|0\.0\.0\.0:3002'; then
|
||||
if bash -c "
|
||||
$assert_port_free_fn
|
||||
assert_port_free 3002 candidate >/dev/null 2>&1
|
||||
"; then
|
||||
fail "an occupied port must be rejected, but assert_port_free returned success"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo 'Deploy port-selection tests passed'
|
||||
+221
-32
@@ -76,6 +76,13 @@ healthy() {
|
||||
return 1
|
||||
}
|
||||
|
||||
# Zelfde check als `healthy`, maar zonder retries. Voor het bepalen van de
|
||||
# actieve poort willen we geen 90 seconden per slot wachten: daar gaat het om
|
||||
# een al draaiend proces dat nu of nooit antwoordt.
|
||||
answers_health() {
|
||||
curl -sf --max-time 5 "http://127.0.0.1:$1/api/health" | grep -q '"database":true'
|
||||
}
|
||||
|
||||
# Staat er een blue/green-upstream? Zonder die bestanden blijft dit script op de
|
||||
# oude, in-place cutover vallen, zodat een host met een andere nginx-indeling
|
||||
# niet stilvalt op een upgrade.
|
||||
@@ -85,29 +92,156 @@ detect_blue_green() {
|
||||
return 0
|
||||
}
|
||||
|
||||
# Welke poort is op dit moment ÉCHT live? Kijk niet naar het upstream-bestand
|
||||
# (dat kan door een losse `docker compose up` zijn ingehaald en naar een dood
|
||||
# slot wijzen), maar test welk slot werkelijk antwoordt op /api/health. Alleen
|
||||
# in een dubbelzinnige situatie (geen óf beide slots gezond) valt het script
|
||||
# terug op de huidige nginx-pointer; onbekend = slot A (eerste release op 3002).
|
||||
# Welke poort is op dit moment ÉCHT live?
|
||||
#
|
||||
# Volgorde van vertrouwen:
|
||||
# 1. Het nginx-upstream-bestand. Dat is de enige bron die aangeeft wáár het
|
||||
# publieke verkeer daadwerkelijk binnenkomt; alles daaronder is gevolg.
|
||||
# 2. Een gezond slot dat overeenkomt met die aanwijzing.
|
||||
# 3. Precies één gezond slot (een verse host met geen upstream-bestand).
|
||||
#
|
||||
# De eerdere versie telde gezonde slots en gebruikte de fallback pas als er 0 of
|
||||
# 2+ waren. Op een host waar beide slots tegelijk gezond zijn — bijvoorbeeld
|
||||
# doordat een losse `docker compose up` een extra replica heeft achtergelaten —
|
||||
# gaf dat een willekeurige keuze, en dan kon de kandidaat op een bezette poort
|
||||
# starten (EADDRINUSE) terwijl de health-check de reeds draaiende container op
|
||||
# die poort beantwoordde. De release-vergelijking faalde dan 30 keer op een
|
||||
# container die toevallig een andere release draaide.
|
||||
read_active_port() {
|
||||
local live="" port="" result=""
|
||||
local count=0
|
||||
for port in "$slot_a_port" "$slot_b_port"; do
|
||||
if curl -sf --max-time 3 "http://127.0.0.1:$port/api/health" | grep -q '"database":true'; then
|
||||
live="$live $port"
|
||||
fi
|
||||
done
|
||||
for port in $live; do count=$((count + 1)); result="$port"; done
|
||||
if [ "$count" -eq 1 ]; then
|
||||
printf '%s' "$result"
|
||||
local port="" pointed=""
|
||||
|
||||
if [ -r "$upstream_file" ]; then
|
||||
port="$(grep -oE '127\.0\.0\.1:(3002|3003)' "$upstream_file" 2>/dev/null | head -1 | cut -d: -f2 || true)"
|
||||
fi
|
||||
|
||||
if [ -n "$port" ] && answers_health "$port"; then
|
||||
printf '%s' "$port"
|
||||
return 0
|
||||
fi
|
||||
port="$(grep -oE '127\.0\.0\.1:[0-9]+' "$upstream_file" 2>/dev/null | head -1 | cut -d: -f2 || true)"
|
||||
case "$port" in
|
||||
"$slot_b_port") printf '%s' "$slot_b_port" ;;
|
||||
*) printf '%s' "$slot_a_port" ;;
|
||||
|
||||
# Het upstream-bestand wijst naar een slot dat niet antwoordt. Kies dan het
|
||||
# enige andere gezonde slot, anders is er niets om op te bouwen.
|
||||
for candidate in "$slot_a_port" "$slot_b_port"; do
|
||||
[ "$candidate" = "$port" ] && continue
|
||||
if answers_health "$candidate"; then
|
||||
echo "nginx points at ${port:-unknown}, which is unhealthy; ${candidate} answers instead" >&2
|
||||
printf '%s' "$candidate"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
# Geen enkel slot antwoordt. Vertrouw dan op het bestand, zodat een
|
||||
# rollback-poging toch het vorige slot kan starten.
|
||||
if [ -n "$port" ]; then
|
||||
printf '%s' "$port"
|
||||
return 0
|
||||
fi
|
||||
|
||||
printf '%s' "$slot_a_port"
|
||||
}
|
||||
|
||||
# Poort-bezetting controleren vóór het starten van de kandidaat.
|
||||
#
|
||||
# Zonder deze check zorgt `docker run` er stilzwijgend voor dat de kandidaat
|
||||
# dood gaat op EADDRINUSE, terwijl de health-check ondertussen de reeds draaiende
|
||||
# container op diezelfde poort beantwoordt. Dat levert een misleidende
|
||||
# "expected release never became healthy" op in plaats van de echte oorzaak.
|
||||
# Elke listener wordt hierboven concreet genoemd, inclusief de container die
|
||||
# hem vasthoudt.
|
||||
assert_port_free() {
|
||||
local port="$1" name="$2"
|
||||
local holders=""
|
||||
# `type`, niet `command -v`: de deploy-simulatietests leveren `ss` als
|
||||
# shell-functie via BASH_ENV, en `command -v` herkent die wel op Bash maar de
|
||||
# functie is niet geëxporteerd naar de subshell van start_candidate. Met `type`
|
||||
# blijft de stub ook daar zichtbaar, zodat de test geen echte hostpoorten
|
||||
# hoeft te zien.
|
||||
if type ss >/dev/null 2>&1; then
|
||||
# `ss` drukt altijd een kolomkop af, ook als er geen listener is. Filter op
|
||||
# LISTEN, anders zou elke vrije poort als bezet gemeld worden.
|
||||
holders="$(ss -ltnp "sport = :$port" 2>/dev/null | grep -F 'LISTEN' || true)"
|
||||
fi
|
||||
[ -z "$holders" ] && return 0
|
||||
echo "Port $port is already in use, cannot start candidate $name" >&2
|
||||
printf '%s\n' "$holders" >&2
|
||||
|
||||
# Noem exact het container dat de poort vasthoudt.
|
||||
#
|
||||
# `docker ps --filter publish=` werkt niet: de app draait met --net=host en
|
||||
# publiceert dus geen poorten, dus die filter levert altijd niets op. In plaats
|
||||
# daarvan volgen we de luisterende PID uit `ss` terug naar de container via
|
||||
# /proc/<pid>/cgroup. Een eerdere versie noemde álle draaiende containers als
|
||||
# belkenners, wat de echte boosdochter (epicnext-cms) onder een zee van
|
||||
# onschuldige containers begraven.
|
||||
local squatter="squatter_pids"
|
||||
squatter_pids="$(printf '%s\n' "$holders" | grep -oP 'pid=\K[0-9]+' | sort -u || true)"
|
||||
if [ -n "$squatter_pids" ]; then
|
||||
local pid cid owner=""
|
||||
for pid in $squatter_pids; do
|
||||
cid="$(sed -n 's#.*docker-\([0-9a-f]\{64\}\)\.scope#\1#p' "/proc/$pid/cgroup" 2>/dev/null | head -1)"
|
||||
[ -n "$cid" ] || continue
|
||||
owner="$(docker inspect --format '{{.Name}} ({{.Config.Image}})' "$cid" 2>/dev/null || true)"
|
||||
[ -n "$owner" ] && printf 'Held by container: %s\n' "${owner#/}" >&2
|
||||
done
|
||||
fi
|
||||
echo "" >&2
|
||||
# Blauwe/groene releases beheren hun eigen slots. Een container met een andere
|
||||
# naam die toevallig op een van deze poorten draait — meestal een
|
||||
# `docker compose up`-replica — staat los van de pipeline en blokkeert de
|
||||
# release. Live verkeer loopt via het nginx-upstream over het andere slot en is
|
||||
# dus niet geraakt.
|
||||
case "$owner" in
|
||||
*"/$name"*|*"/$slot_b_container"*)
|
||||
echo "Note: the holder looks like a managed slot container; re-check the port mapping above." >&2 ;;
|
||||
*)
|
||||
cat >&2 <<EOF
|
||||
This port is held by a container that is not a blue/green slot, so the deploy
|
||||
cannot start the candidate. Live traffic is unaffected: nginx keeps serving
|
||||
the other slot until cutover.
|
||||
|
||||
Remove the stray container and re-run the deploy:
|
||||
|
||||
docker rm -f $(printf '%s' "$owner" | sed -n 's#.*/\([^ ]*\).*#\1#p')
|
||||
|
||||
If it comes back after a reboot, it is started by docker-compose.yml rather
|
||||
than by this script; delete or disable that service.
|
||||
EOF
|
||||
;;
|
||||
esac
|
||||
return 1
|
||||
}
|
||||
|
||||
# Ruim een compose-replica op die een blauwe/groene slot bezet.
|
||||
#
|
||||
# Een `docker compose up` — of de dagelijkse `scripts/docker-update.sh`, waarvan
|
||||
# de CI-eigendomscontrole per slot wankelde — laat een replica met container_name
|
||||
# `epicnext-cms` achter op poort 3002. Die draait nooit live: nginx wijst naar de
|
||||
# poort van een slot-container die dit script zelf heeft gestart, en die staat per
|
||||
# definitie aan de andere kant dan de kandidaat. Zonder deze opruimstap loopt elke
|
||||
# release vast op een bezette poort totdat iemand de container met de hand
|
||||
# verwijdert.
|
||||
#
|
||||
# Bewust smal, want een container van een ander deployment is niet van ons:
|
||||
# - alleen een replica die uit precies deze checkout komt
|
||||
# (com.docker.compose.project.config_files), niet een losse compose-project;
|
||||
# - nooit een slot-container, want die beheert dit script zelf;
|
||||
# - nooit de poort waar nginx naar wijst.
|
||||
# Wat daarnaast nog op de doel-poort zit, laat assert_port_free() met zijn eigen
|
||||
# foutmelding staan in plaats van stilzwijgend verdwijnen.
|
||||
retire_compose_replicas() {
|
||||
local live_port="$1" cid name="" config_files="" port=""
|
||||
while read -r cid; do
|
||||
[ -n "$cid" ] || continue
|
||||
name="$(docker inspect --format '{{.Name}}' "$cid" 2>/dev/null | sed -n 's#^/##p' || true)"
|
||||
case "$name" in ''|"$slot_a_container"|"$slot_b_container") continue ;; esac
|
||||
config_files="$(docker inspect --format '{{index .Config.Labels "com.docker.compose.project.config_files"}}' "$cid" 2>/dev/null || true)"
|
||||
[ "$config_files" = "$deploy_dir/docker-compose.yml" ] || continue
|
||||
port="$(docker inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$cid" 2>/dev/null | sed -n 's#^PORT=##p' | head -1 || true)"
|
||||
[ -n "$port" ] && [ "$port" != "$live_port" ] || continue
|
||||
echo "Removing compose replica $name on port $port: it squats a blue/green slot and is not the live release (nginx serves $live_port)"
|
||||
docker rm -f "$name" || return 1
|
||||
done < <(docker ps --filter "label=com.docker.compose.project.config_files=$deploy_dir/docker-compose.yml" --format '{{.ID}}')
|
||||
return 0
|
||||
}
|
||||
|
||||
# Zet de nginx-upstream op de nieuwe poort en herlaadt graceful.
|
||||
@@ -153,6 +287,7 @@ switch_upstream() {
|
||||
# gelden.
|
||||
start_candidate() {
|
||||
local port="$1" name="$2"
|
||||
assert_port_free "$port" "$name"
|
||||
(
|
||||
set -a
|
||||
# shellcheck disable=SC1091
|
||||
@@ -183,7 +318,7 @@ finish() {
|
||||
if [ "$cutover_started" -eq 0 ]; then
|
||||
# De live release draait nog ongestoord; alleen de kandidaat opruimen.
|
||||
echo "Deployment failed before cutover; the live release was never stopped" >&2
|
||||
if [ "$candidate_attempted" -eq 1 ] && [ -n "$new_container" ]; then
|
||||
if [ "$candidate_attempted" -eq 1 ] && [ -n "$new_container" ] && docker inspect "$new_container" >/dev/null 2>&1; then
|
||||
docker logs "$new_container" --tail 50 >&2 || true
|
||||
docker rm -f "$new_container" || true
|
||||
fi
|
||||
@@ -191,9 +326,9 @@ finish() {
|
||||
# nginx wijst nu naar de kandidaat. Eerst het verkeer terug, dan pas de
|
||||
# kandidaat weghalen, anders zou de site 502-en terwijl we terugdraaien.
|
||||
echo "Deployment failed after cutover; rolling back to port $old_port" >&2
|
||||
if [ -n "$new_container" ]; then docker logs "$new_container" --tail 50 >&2 || true; fi
|
||||
if [ -n "$new_container" ] && docker inspect "$new_container" >/dev/null 2>&1; then docker logs "$new_container" --tail 50 >&2 || true; fi
|
||||
if [ -n "$old_port" ]; then switch_upstream "$old_port" || true; fi
|
||||
if [ -n "$new_container" ]; then docker rm -f "$new_container" || true; fi
|
||||
if [ -n "$new_container" ] && docker inspect "$new_container" >/dev/null 2>&1; then docker rm -f "$new_container" || true; fi
|
||||
if [ -n "$old_container" ] && docker start "$old_container" >/dev/null 2>&1; then
|
||||
if healthy "$old_port"; then
|
||||
echo "Rollback verified on port $old_port"
|
||||
@@ -260,11 +395,30 @@ if ! grep -qs '^DATABASE_URL=' .env; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# De image krijgt het label van $sha, en `verify-deployed-release.mjs` controleert
|
||||
# later alleen díe label. Zonder deze check bouwt een vuile werkboom dus een image
|
||||
# die zegt release $sha te zijn terwijl er ongecommitte code in zit — precies het
|
||||
# scenario "rebuilden levert geen nieuwe code". `docker-update.sh` deed dit al.
|
||||
# `--untracked-files=normal` laat gitignored artefacten (.next, coverage,
|
||||
# build-reports) buiten beschouwing; die worden toch niet meegebouwd.
|
||||
if [ -n "$(git status --porcelain --untracked-files=normal)" ]; then
|
||||
echo "Error: de werkboom is niet schoon, dus de image zou een verkeerd release-label krijgen." >&2
|
||||
echo " Commit of stash de wijzigingen en draai opnieuw." >&2
|
||||
echo " De live release is niet aangeraakt." >&2
|
||||
git status --short >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
pnpm install --frozen-lockfile
|
||||
pnpm exec playwright install chromium
|
||||
# All three engines are needed: the chromium gate below plus the cross-browser
|
||||
# imaging gate just before cutover. Idempotent — already-installed engines in
|
||||
# PLAYWRIGHT_BROWSERS_PATH are detected and skipped.
|
||||
pnpm exec playwright install chromium firefox webkit
|
||||
|
||||
echo "Building $image"
|
||||
DOCKER_BUILDKIT=1 docker build --network=host --progress=plain --cache-from epicnext-cms:latest \
|
||||
# No --network=host: BuildKit only grants it via --allow=network.host, and the
|
||||
# build needs nothing but outbound internet (apk, pnpm, next/font/google).
|
||||
DOCKER_BUILDKIT=1 docker build --progress=plain --cache-from epicnext-cms:latest \
|
||||
--build-arg NEXT_DEPLOYMENT_ID="$sha" -t "$image" .
|
||||
check_current
|
||||
# Read reports from the already-built image; do not start an extra application.
|
||||
@@ -338,15 +492,28 @@ if docker inspect "$backup_name" >/dev/null 2>&1; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# The avatar/badge disk cache lives on the host bind and is written by uid 33
|
||||
# inside the container. Root-owned directories make every cache write fail
|
||||
# silently, which turns each avatar into a fresh live render.
|
||||
# The application writes everything under storage/ as uid 33, but storage is a
|
||||
# host bind so the image's own ownership is irrelevant. Any path that is not
|
||||
# uid 33 makes the write fail with EACCES, and because most of these writes are
|
||||
# inside a try/catch the failure is silent: the avatar cache just never fills
|
||||
# (each avatar becomes a fresh live render) and the catalog export reports
|
||||
# "delivery failed" while the emulator never receives the update. The old code
|
||||
# only repaired storage/imaging, so storage/catalog-git/hotel-status.json kept
|
||||
# coming back root:root and /api/admin/catalog/status kept throwing EACCES.
|
||||
for owned_dir in imaging catalog-git cms-errors furniture-imports logs media \
|
||||
nitro-cleanup config-backups nitro-scale32-backups; do
|
||||
target="$deploy_dir/storage/$owned_dir"
|
||||
[ -e "$target" ] || mkdir -p "$target" 2>/dev/null || true
|
||||
[ -d "$target" ] || continue
|
||||
chown -R 33:33 "$target" 2>/dev/null || true
|
||||
done
|
||||
# The avatar/badge cache needs its leaf directories to exist before first use;
|
||||
# the cache misses (and re-renders live) rather than erroring when they do not.
|
||||
for cache_dir in avatars badges; do
|
||||
if ! install -d -o 33 -g 33 -m 0750 "$deploy_dir/storage/imaging/$cache_dir" 2>/dev/null; then
|
||||
mkdir -p "$deploy_dir/storage/imaging/$cache_dir" 2>/dev/null || true
|
||||
fi
|
||||
done
|
||||
chown -R 33:33 "$deploy_dir/storage/imaging" 2>/dev/null || true
|
||||
|
||||
if [ "$blue_green" -eq 1 ]; then
|
||||
# 1. Maak de doel-poort vrij. Alles wat daar draait is per definitie niet live,
|
||||
@@ -356,23 +523,40 @@ if [ "$blue_green" -eq 1 ]; then
|
||||
docker rm -f "$new_container"
|
||||
fi
|
||||
|
||||
# 2. Start de kandidaat ernaast. De live release draait ononderbroken door.
|
||||
# 2. Een compose-replica die ooit is achtergebleven zit hier nog op de
|
||||
# doel-poort. Hij draait niet live en wordt dus opgeruimd, zodat de release
|
||||
# niet op een bezette poort stukloopt.
|
||||
retire_compose_replicas "$old_port"
|
||||
|
||||
# 3. Start de kandidaat ernaast. De live release draait ononderbroken door.
|
||||
candidate_attempted=1
|
||||
start_candidate "$new_port" "$new_container"
|
||||
|
||||
# 3. Gezond? Release-hash klopt? Browsersmoke-test? Pas dan hoeft het oude
|
||||
# 4. Gezond? Release-hash klopt? Browsersmoke-test? Pas dan hoeft het oude
|
||||
# release het veld te ruimen — anders zou een mislukte e2e-test pas ná de
|
||||
# cutover de productie breken in plaats van ervoor.
|
||||
healthy "$new_port"
|
||||
node scripts/verify-deployed-release.mjs "http://127.0.0.1:$new_port/api/health" "$sha"
|
||||
PLAYWRIGHT_BASE_URL="http://127.0.0.1:$new_port" pnpm test:e2e
|
||||
|
||||
# 4. Het enige onomkeerbare moment: vanaf hier wijst nginx naar de kandidaat.
|
||||
# 4b. Cross-browser imaging gate. The chromium suite above proves the
|
||||
# candidate serves the right bytes; this proves every bundled engine can
|
||||
# actually decode the render. The webkit path is the one that historically
|
||||
# broke (CSP upgrade-insecure-requests + animated PNG), and a regression
|
||||
# there must never reach live. Set DEPLOY_SKIP_CROSS_BROWSER=1 only to get
|
||||
# a release out in an emergency.
|
||||
if [ "${DEPLOY_SKIP_CROSS_BROWSER:-0}" = "1" ]; then
|
||||
echo "WARNING: cross-browser imaging gate skipped (DEPLOY_SKIP_CROSS_BROWSER=1)" >&2
|
||||
else
|
||||
PLAYWRIGHT_BASE_URL="http://127.0.0.1:$new_port" pnpm test:imaging
|
||||
fi
|
||||
|
||||
# 5. Het enige onomkeerbare moment: vanaf hier wijst nginx naar de kandidaat.
|
||||
cutover_started=1
|
||||
switch_upstream "$new_port"
|
||||
echo "Cut over to port $new_port; retiring port $old_port"
|
||||
|
||||
# 5. Nu mag de oude release weg. Pas ná de swap, zodat er nooit een moment is
|
||||
# 6. Nu mag de oude release weg. Pas ná de swap, zodat er nooit een moment is
|
||||
# waarop er geen enkele container draait.
|
||||
if [ -n "$old_container" ] && docker inspect "$old_container" >/dev/null 2>&1; then
|
||||
docker stop "$old_container"
|
||||
@@ -400,6 +584,11 @@ else
|
||||
healthy 3002
|
||||
node scripts/verify-deployed-release.mjs http://127.0.0.1:3002/api/health "$sha"
|
||||
PLAYWRIGHT_BASE_URL=http://127.0.0.1:3002 pnpm test:e2e
|
||||
if [ "${DEPLOY_SKIP_CROSS_BROWSER:-0}" = "1" ]; then
|
||||
echo "WARNING: cross-browser imaging gate skipped (DEPLOY_SKIP_CROSS_BROWSER=1)" >&2
|
||||
else
|
||||
PLAYWRIGHT_BASE_URL=http://127.0.0.1:3002 pnpm test:imaging
|
||||
fi
|
||||
verified_image="$(docker inspect --format '{{.Image}}' epicnext-cms-app)"
|
||||
fi
|
||||
docker tag "$verified_image" "epicnext-cms:verified-$sha"
|
||||
|
||||
@@ -39,6 +39,6 @@ pnpm exec playwright install chromium
|
||||
export NEWS_E2E_IMAGE="$image"
|
||||
export NEWS_E2E_RELEASE="$sha"
|
||||
build_attempted=1
|
||||
DOCKER_BUILDKIT=1 docker build --network=host --progress=plain \
|
||||
DOCKER_BUILDKIT=1 docker build --progress=plain \
|
||||
--build-arg NEXT_DEPLOYMENT_ID="$sha" -t "$image" .
|
||||
NEWS_E2E_IMAGE="$image" NEWS_E2E_RELEASE="$sha" node --import tsx e2e/news-real/run.ts
|
||||
@@ -1,154 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
cd "$DIR"
|
||||
ENV_FILE="$DIR/.env"
|
||||
COMPOSE_FILE="deployment/crowdsec/compose.crowdsec.yml"
|
||||
PROJECT_NAME="epicnext-crowdsec"
|
||||
CONTAINER_NAME="epicnext-crowdsec"
|
||||
DEFAULT_PORT="18080"
|
||||
|
||||
mode="${1:-enable}"
|
||||
case "$mode" in
|
||||
enable|--enable) ;;
|
||||
status|--status) ;;
|
||||
disable|--disable) ;;
|
||||
blocklists|blocklists-install-cron|blocklists-uninstall-cron) ;;
|
||||
*) echo "Usage: bash cms security [enable|status|disable|blocklists|blocklists-install-cron|blocklists-uninstall-cron]" >&2; exit 1 ;;
|
||||
esac
|
||||
|
||||
umask 077
|
||||
fail() { printf 'ERROR: %s\n' "$*" >&2; exit 1; }
|
||||
for command in docker flock; do command -v "$command" >/dev/null || fail "Required command: $command"; done
|
||||
docker info >/dev/null 2>&1 || fail "Docker is not reachable."
|
||||
docker compose version >/dev/null 2>&1 || fail "Docker Compose plugin required."
|
||||
exec 9>"$DIR/.deploy.lock"
|
||||
flock -w 30 9 || fail "Another installation or update is running."
|
||||
[[ -f "$ENV_FILE" ]] || fail "Create .env first (bash cms install)."
|
||||
|
||||
case "$mode" in
|
||||
blocklists) exec bash "$DIR/scripts/blocklists-sync.sh" sync "${2:-}" ;;
|
||||
blocklists-install-cron) exec bash "$DIR/scripts/blocklists-sync.sh" install-cron ;;
|
||||
blocklists-uninstall-cron) exec bash "$DIR/scripts/blocklists-sync.sh" uninstall-cron ;;
|
||||
esac
|
||||
|
||||
env_get() {
|
||||
local key="$1" line
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
case "$line" in
|
||||
"$key="*) line="${line#*=}"; line="${line%\"}"; line="${line#\"}"; printf '%s' "$line"; return 0 ;;
|
||||
esac
|
||||
done < "$ENV_FILE"
|
||||
return 1
|
||||
}
|
||||
|
||||
env_set() {
|
||||
local key="$1" value="$2" tmp
|
||||
tmp="$(mktemp "$DIR/.env.crowdsec.XXXXXX")"
|
||||
if awk -v k="$key" -v v="$value" 'BEGIN{FS=OFS="=";done=0} { if ($1==k) { print k "=" v; done=1 } else print } END { if (!done) print k "=" v }' "$ENV_FILE" > "$tmp"; then
|
||||
chmod 600 "$tmp"
|
||||
mv -f -- "$tmp" "$ENV_FILE"
|
||||
else
|
||||
rm -f -- "$tmp"
|
||||
fail "Could not update .env"
|
||||
fi
|
||||
}
|
||||
|
||||
compose_cmd() {
|
||||
docker compose --project-name "$PROJECT_NAME" --env-file "$ENV_FILE" -f "$COMPOSE_FILE" --profile security "$@"
|
||||
}
|
||||
|
||||
health_probe() {
|
||||
local url="$1"
|
||||
if command -v curl >/dev/null 2>&1; then
|
||||
curl -fsS --max-time 3 "$url" >/dev/null 2>&1
|
||||
else
|
||||
compose_cmd exec -T crowdsec wget -q -O - "$url" >/dev/null 2>&1
|
||||
fi
|
||||
}
|
||||
|
||||
container_running() {
|
||||
[[ "$(docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null || true)" = true ]]
|
||||
}
|
||||
|
||||
if [[ "$mode" = disable || "$mode" = --disable ]]; then
|
||||
set +e
|
||||
compose_cmd stop crowdsec
|
||||
rc=$?
|
||||
set -e
|
||||
[[ $rc -eq 0 ]] || printf 'CrowdSec engine was not running or could not be stopped.\n'
|
||||
env_set CROWDSEC_LOCAL_ENABLED false
|
||||
printf 'CrowdSec local stack disabled. The engine container is stopped; volumes and .env key were kept.\n'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "$mode" = status || "$mode" = --status ]]; then
|
||||
enabled=no
|
||||
[[ "$(env_get CROWDSEC_LOCAL_ENABLED 2>/dev/null || true)" = true ]] && enabled=yes
|
||||
port="$(env_get CROWDSEC_LAPI_PORT 2>/dev/null || true)"
|
||||
[[ -z "$port" ]] && port="$DEFAULT_PORT"
|
||||
printf 'CROWDSEC_LOCAL_ENABLED=%s\n' "$enabled"
|
||||
if container_running; then
|
||||
printf 'Engine: running\n'
|
||||
if health_probe "http://127.0.0.1:$port/health"; then
|
||||
printf 'LAPI health: OK (127.0.0.1:%s)\n' "$port"
|
||||
else
|
||||
printf 'LAPI health: UNREACHABLE (127.0.0.1:%s)\n' "$port"
|
||||
fi
|
||||
else
|
||||
printf 'Engine: not running\n'
|
||||
printf 'Start with: bash cms security\n'
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
port="$(env_get CROWDSEC_LAPI_PORT 2>/dev/null || true)"
|
||||
[[ -n "$port" ]] || port="$DEFAULT_PORT"
|
||||
[[ "$port" =~ ^[0-9]{1,5}$ ]] || fail "CROWDSEC_LAPI_PORT must be a port number."
|
||||
if (( port < 1024 || port > 65535 )); then
|
||||
fail "CROWDSEC_LAPI_PORT must be within 1024-65535."
|
||||
fi
|
||||
key="$(env_get CROWDSEC_LAPI_API_KEY 2>/dev/null || true)"
|
||||
[[ -n "$key" ]] || key="$(od -An -N32 -tx1 /dev/urandom | tr -d ' \n')"
|
||||
url="$(env_get CROWDSEC_LAPI_URL 2>/dev/null || true)"
|
||||
[[ -n "$url" ]] || url="http://127.0.0.1:$port"
|
||||
log_dir="${CROWDSEC_NGINX_LOG_DIR:-$(env_get CROWDSEC_NGINX_LOG_DIR 2>/dev/null || true)}"
|
||||
[[ -n "$log_dir" ]] || log_dir="/var/log/nginx"
|
||||
|
||||
if ! container_running && command -v ss >/dev/null 2>&1; then
|
||||
if ss -ltn "( sport = :$port )" 2>/dev/null | grep -q LISTEN; then
|
||||
fail "Port $port is already in use. Set CROWDSEC_LAPI_PORT (and CROWDSEC_LAPI_URL) in .env to a free port and re-run."
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ ! -r "$log_dir/access.log" ]]; then
|
||||
printf 'Warning: %s/access.log is not readable. The engine will run but has no detections until an access log is available.\n' "$log_dir"
|
||||
fi
|
||||
|
||||
env_set CROWDSEC_LOCAL_ENABLED true
|
||||
env_set CROWDSEC_LAPI_URL "$url"
|
||||
env_set CROWDSEC_LAPI_PORT "$port"
|
||||
env_set CROWDSEC_LAPI_API_KEY "$key"
|
||||
env_set CROWDSEC_NGINX_LOG_DIR "$log_dir"
|
||||
|
||||
compose_cmd config --quiet || fail "CrowdSec Compose configuration is invalid; fix CROWDSEC_* settings in .env."
|
||||
set +e
|
||||
compose_cmd up -d --wait crowdsec
|
||||
rc=$?
|
||||
set -e
|
||||
if [[ $rc -ne 0 ]]; then
|
||||
compose_cmd up -d crowdsec
|
||||
fi
|
||||
|
||||
attempt=0
|
||||
while ! health_probe "http://127.0.0.1:$port/health"; do
|
||||
attempt=$((attempt + 1))
|
||||
[[ $attempt -lt 30 ]] || fail "CrowdSec LAPI did not become healthy on port $port."
|
||||
sleep 2
|
||||
done
|
||||
|
||||
printf 'CrowdSec engine running in LAPI-only mode on 127.0.0.1:%s (container %s).\n' "$port" "$CONTAINER_NAME"
|
||||
compose_cmd exec -T crowdsec cscli bouncers list >/dev/null 2>&1 \
|
||||
&& printf 'Bouncer "cms" was registered against the local LAPI.\n' \
|
||||
|| printf 'Warning: could not list bouncers. Diagnose with: docker compose exec -T %s cscli bouncers list\n' "$CONTAINER_NAME"
|
||||
printf 'Restart the CMS container (or run your next deployment) so it loads the new bouncer env. For a clone: bash cms update --skip-pull\n'
|
||||
+94
-2
@@ -3,15 +3,21 @@
|
||||
#
|
||||
# Modes:
|
||||
# (default) — post-deploy cleanup (safe, fast):
|
||||
# - Build cache older than 72h, capped at 4 GB max used space.
|
||||
# - Build cache capped at 4 GB max used space (CMS_BUILD_CACHE_MAX), evicting
|
||||
# least-recently-used entries. This cap is the actual bound.
|
||||
# - Unreferenced images older than 7 days (keeps rollback images around).
|
||||
# - Stopped containers older than 24h.
|
||||
# - Dangling images, which are always unreferenced.
|
||||
# - Orphaned Firefox profiles in byparr's writable layer (BYPARR_CONTAINERS).
|
||||
# --force — emergency mode ("never let the disk max out"): drops everything
|
||||
# with no age windows:
|
||||
# - ALL unreferenced build cache,
|
||||
# - ALL unreferenced images (no age grace),
|
||||
# - ALL stopped containers.
|
||||
#
|
||||
# The default mode escalates to --force on its own when / drops below 8 GB free,
|
||||
# so the bound holds even if this stops running on schedule.
|
||||
#
|
||||
# Volumes are NEVER pruned in either mode: mariadb-turbo-data is a database.
|
||||
# Idempotent; exits 0 when Docker is unavailable.
|
||||
set -Eeuo pipefail
|
||||
@@ -34,15 +40,101 @@ command -v docker >/dev/null 2>&1 || {
|
||||
printf '\n[%s] === docker prune start%s ===\n' "$(now)" "$( (( FORCE )) && printf ' (FORCE)' )" >>"$LOG_FILE"
|
||||
docker system df >>"$LOG_FILE" 2>&1 || true
|
||||
|
||||
# A hard ceiling on the root filesystem is what actually bounds the growth, so
|
||||
# the emergency path is reached on disk pressure rather than only on a timer.
|
||||
# The image/container passes stay age-gated: a rollback image and a stopped
|
||||
# container are cheap to keep for a week and expensive to lose.
|
||||
FREE_KB=$(df -Pk / | awk 'NR==2 {print $4}')
|
||||
# 8 GB free is comfortable for a database plus a release swap.
|
||||
if (( FREE_KB < 8 * 1024 * 1024 )); then
|
||||
FORCE=1
|
||||
printf '[%s] only %s KB free on /; switching to FORCE prune\n' \
|
||||
"$(now)" "$FREE_KB" >>"$LOG_FILE"
|
||||
fi
|
||||
|
||||
if (( FORCE )); then
|
||||
docker builder prune -af >>"$LOG_FILE" 2>&1 || true
|
||||
docker image prune -af >>"$LOG_FILE" 2>&1 || true
|
||||
docker container prune -f >>"$LOG_FILE" 2>&1 || true
|
||||
else
|
||||
docker builder prune -af --filter "until=72h" --max-used-space=4g >>"$LOG_FILE" 2>&1 || true
|
||||
# --max-used-space and --filter are mutually exclusive in buildx: passing
|
||||
# both makes the cap a no-op and the cache grows without bound. The cap alone
|
||||
# is the bound, and it evicts least-recently-used entries to get there.
|
||||
docker builder prune -af --max-used-space="${CMS_BUILD_CACHE_MAX:-4g}" >>"$LOG_FILE" 2>&1 || true
|
||||
docker image prune -af --filter "until=168h" >>"$LOG_FILE" 2>&1 || true
|
||||
docker container prune -f --filter "until=24h" >>"$LOG_FILE" 2>&1 || true
|
||||
fi
|
||||
|
||||
# Dangling images have no tag and no container, so nothing can reference them.
|
||||
# They are what repeated local builds leave behind.
|
||||
docker image prune -f >>"$LOG_FILE" 2>&1 || true
|
||||
|
||||
# ── Interrupted git gc leftovers ─────────────────────────────────
|
||||
# A `git gc` that gets OOM-killed mid-repack leaves its tmp_pack behind, and
|
||||
# nothing reclaims it: git only clears those on the next successful gc. One such
|
||||
# file held 7.7 GB here while the whole object store was 83 MB. Only files older
|
||||
# than a day are considered, so a gc running right now is never touched.
|
||||
repo_dir="${CMS_REPO_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
|
||||
if [[ -d "$repo_dir/.git/objects/pack" ]]; then
|
||||
while IFS= read -r -d '' tmp; do
|
||||
size=$(du -h "$tmp" | cut -f1)
|
||||
rm -f "$tmp"
|
||||
printf '[%s] removed leftover tmp_pack %s (%s) from an interrupted git gc\n' \
|
||||
"$(now)" "$tmp" "$size" >>"$LOG_FILE"
|
||||
done < <(find "$repo_dir/.git/objects/pack" -maxdepth 1 -name 'tmp_*' -mmin +1440 -print0 2>/dev/null)
|
||||
fi
|
||||
|
||||
# ── Orphaned browser profiles ─────────────────────────────────────
|
||||
# byparr launches a real Firefox per request, and each launch leaves a
|
||||
# ~10-140 MB profile behind in the container's writable layer. Nothing ever
|
||||
# removes them, so the layer grows without bound: 716 profiles / 6.8 GB after two
|
||||
# days on this host, ~1.7 GB/day.
|
||||
#
|
||||
# Deleting a profile out from under a running browser kills that job, so live
|
||||
# ones are identified the only way that is reliable rather than by age: a
|
||||
# browser keeps its profile open, which shows up as a /proc/<pid>/fd symlink
|
||||
# pointing into the directory. Anything not referenced that way, and untouched
|
||||
# for BYPARR_PROFILE_MIN_AGE_MIN minutes, is an orphan.
|
||||
#
|
||||
# Age alone is not a safe signal here: browsers stay warm for ~27 hours, so an
|
||||
# age window that is safe for the leak is far too wide for the disk.
|
||||
BYPARR_TMP_MIN_AGE_MIN="${BYPARR_TMP_MIN_AGE_MIN:-30}"
|
||||
for container in ${BYPARR_CONTAINERS:-byparr}; do
|
||||
docker inspect -f '{{.State.Running}}' "$container" >/dev/null 2>&1 || continue
|
||||
[[ "$(docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null)" == "true" ]] || continue
|
||||
|
||||
removed=$(
|
||||
docker exec -e BYPARR_TMP_MIN_AGE_MIN="$BYPARR_TMP_MIN_AGE_MIN" "$container" sh -c '
|
||||
set -u
|
||||
min_age="${BYPARR_TMP_MIN_AGE_MIN:-30}"
|
||||
base="${1:-/tmp}"
|
||||
live_file=$(mktemp)
|
||||
# Live profiles are the ones a running process still holds open.
|
||||
for p in $(ps -eo pid= 2>/dev/null); do
|
||||
ls -l "/proc/$p/fd" 2>/dev/null
|
||||
done | grep -o "$base/playwright_firefoxdev_profile-[A-Za-z0-9]*" | sort -u >"$live_file"
|
||||
|
||||
count=0
|
||||
for dir in "$base"/playwright_firefoxdev_profile-*; do
|
||||
[ -d "$dir" ] || continue
|
||||
# Never touch something a process is still using.
|
||||
grep -Fxq "$dir" "$live_file" && continue
|
||||
# A profile a browser is still writing to is not an orphan
|
||||
# yet, even if the directory itself looks old.
|
||||
if find "$dir" -newermt "-${min_age} minutes" -print -quit 2>/dev/null | grep -q .; then
|
||||
continue
|
||||
fi
|
||||
rm -rf "$dir" 2>/dev/null && count=$((count + 1))
|
||||
done
|
||||
rm -f "$live_file"
|
||||
printf "%s" "$count"
|
||||
' sh /tmp 2>/dev/null || printf '0'
|
||||
)
|
||||
if [[ "${removed:-0}" -gt 0 ]]; then
|
||||
printf '[%s] removed %s orphaned browser profiles from %s\n' \
|
||||
"$(now)" "$removed" "$container" >>"$LOG_FILE"
|
||||
fi
|
||||
done
|
||||
|
||||
printf '\n[%s] === docker prune complete%s ===\n' "$(now)" "$( (( FORCE )) && printf ' (FORCE)' )" >>"$LOG_FILE"
|
||||
docker system df >>"$LOG_FILE" 2>&1 || true
|
||||
@@ -1,7 +1,13 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
import { it } from "vitest";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
detectMemoryLimitMb,
|
||||
heapLimitMb,
|
||||
runtimeNodeOptions,
|
||||
} from "./docker-start.mjs";
|
||||
|
||||
it("imports runtime validation without starting the CMS", () => {
|
||||
const result = spawnSync(
|
||||
@@ -16,28 +22,88 @@ it("imports runtime validation without starting the CMS", () => {
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
});
|
||||
|
||||
it("rejects the local CrowdSec bouncer without a key", () => {
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
"--input-type=module",
|
||||
"-e",
|
||||
"import {validateRuntime} from './scripts/docker-start.mjs';try{validateRuntime({HOTEL_NAME:'x',AUTH_SECRET:'01234567890123456789012345678901',DATABASE_URL:'mysql://u:p@h/db',APP_URL:'http://h',CROWDSEC_LOCAL_ENABLED:'true'});process.exit(1)}catch(error){if(!String(error.message).includes('CROWDSEC_LAPI_API_KEY'))throw error}",
|
||||
],
|
||||
{ encoding: "utf8" },
|
||||
);
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
describe("heap limit", () => {
|
||||
it("leaves headroom for the memory V8 does not account for", () => {
|
||||
// 4 GB cgroup limit -> a 2867 MB heap, well under the ceiling.
|
||||
expect(heapLimitMb(4 * 1024 ** 3)).toBe(2867);
|
||||
expect(heapLimitMb(6 * 1024 ** 3)).toBe(4300);
|
||||
});
|
||||
|
||||
it("clamps to a floor and a ceiling", () => {
|
||||
// Too small to run a Next.js server at all: floor wins.
|
||||
expect(heapLimitMb(256 * 1024 ** 2)).toBe(512);
|
||||
// A huge or absent limit must not turn into a 100 GB heap.
|
||||
expect(heapLimitMb(64 * 1024 ** 3)).toBe(8192);
|
||||
expect(heapLimitMb(Number.NaN)).toBe(8192);
|
||||
expect(heapLimitMb(0)).toBe(8192);
|
||||
});
|
||||
});
|
||||
|
||||
it("accepts a complete local CrowdSec configuration", () => {
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
"--input-type=module",
|
||||
"-e",
|
||||
"import {validateRuntime} from './scripts/docker-start.mjs';validateRuntime({HOTEL_NAME:'x',AUTH_SECRET:'01234567890123456789012345678901',DATABASE_URL:'mysql://u:p@h/db',APP_URL:'http://h',CROWDSEC_LOCAL_ENABLED:'true',CROWDSEC_LAPI_API_KEY:'fixture-key',CROWDSEC_LAPI_URL:'http://127.0.0.1:18080'})",
|
||||
],
|
||||
{ encoding: "utf8" },
|
||||
);
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
describe("cgroup detection", () => {
|
||||
const asReader = (contents) => (path) => {
|
||||
if (!(path in contents)) throw new Error(`ENOENT: ${path}`);
|
||||
return contents[path];
|
||||
};
|
||||
|
||||
it("reads the cgroup v2 limit", () => {
|
||||
expect(
|
||||
detectMemoryLimitMb(
|
||||
asReader({ "/sys/fs/cgroup/memory.max": "4294967296" }),
|
||||
),
|
||||
).toBe(2867);
|
||||
});
|
||||
|
||||
it("falls back to cgroup v1 when v2 is absent", () => {
|
||||
expect(
|
||||
detectMemoryLimitMb(
|
||||
asReader({
|
||||
"/sys/fs/cgroup/memory.max": "",
|
||||
"/sys/fs/cgroup/memory/memory.limit_in_bytes": "6442450944",
|
||||
}),
|
||||
),
|
||||
).toBe(4300);
|
||||
});
|
||||
|
||||
it("treats an unlimited cgroup as no limit at all", () => {
|
||||
// cgroup v1 reports "max"; a bare sentinel means the same thing.
|
||||
expect(
|
||||
detectMemoryLimitMb(asReader({ "/sys/fs/cgroup/memory.max": "max" })),
|
||||
).toBe(8192);
|
||||
expect(
|
||||
detectMemoryLimitMb(
|
||||
asReader({
|
||||
"/sys/fs/cgroup/memory/memory.limit_in_bytes": "9223372036854771712",
|
||||
}),
|
||||
),
|
||||
).toBe(8192);
|
||||
});
|
||||
|
||||
it("falls back when neither cgroup file is readable", () => {
|
||||
expect(
|
||||
detectMemoryLimitMb(() => {
|
||||
throw new Error("ENOENT");
|
||||
}),
|
||||
).toBe(8192);
|
||||
});
|
||||
});
|
||||
|
||||
describe("NODE_OPTIONS", () => {
|
||||
it("adds the cap when none is set", () => {
|
||||
expect(runtimeNodeOptions("", 2867)).toBe("--max-old-space-size=2867");
|
||||
expect(runtimeNodeOptions(undefined, 2867)).toBe(
|
||||
"--max-old-space-size=2867",
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps unrelated options already present", () => {
|
||||
expect(runtimeNodeOptions("--no-warnings", 2867)).toBe(
|
||||
"--no-warnings --max-old-space-size=2867",
|
||||
);
|
||||
});
|
||||
|
||||
it("never overrides an explicit operator choice", () => {
|
||||
expect(runtimeNodeOptions("--max-old-space-size=8192", 2867)).toBe(
|
||||
"--max-old-space-size=8192",
|
||||
);
|
||||
});
|
||||
});
|
||||
+70
-17
@@ -1,7 +1,70 @@
|
||||
// Fail before listening if an installation has no valid runtime configuration.
|
||||
import { spawn } from "node:child_process";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { pathToFileURL } from "node:url";
|
||||
|
||||
/** Fraction of the container memory limit V8 is allowed to use for its heap.
|
||||
* The rest has to cover native allocations the JS heap cannot account for:
|
||||
* the mysql2 pool buffers, sharp's image pipeline, and zlib during a burst of
|
||||
* RSC rendering. */
|
||||
const HEAP_FRACTION = 0.7;
|
||||
const MIN_HEAP_MB = 512;
|
||||
/** Backstop only. The fraction is the real policy: on a 6 GB container it asks
|
||||
* for 4300 MB, and a backstop at or below that would silently turn the fraction
|
||||
* into a fixed number and make the two limits disagree. This exists purely so a
|
||||
* nonsensical cgroup reading cannot ask for an unbounded heap. */
|
||||
const MAX_HEAP_MB = 8192;
|
||||
|
||||
export function heapLimitMb(cgroupLimitBytes) {
|
||||
if (!Number.isFinite(cgroupLimitBytes) || cgroupLimitBytes <= 0)
|
||||
return MAX_HEAP_MB;
|
||||
const mb = Math.floor((cgroupLimitBytes * HEAP_FRACTION) / (1024 * 1024));
|
||||
return Math.min(MAX_HEAP_MB, Math.max(MIN_HEAP_MB, mb));
|
||||
}
|
||||
|
||||
/**
|
||||
* Read this container's memory ceiling from cgroup v2, falling back to v1.
|
||||
* Without this the V8 heap defaults to a quarter of *host* memory, so a 4 GB
|
||||
* container on a 24 GB host lets the heap grow past the limit and the kernel
|
||||
* OOM-kills the process mid-request — which is what produced the
|
||||
* `next-build (v16)` kills in the host logs. A container that GCs before it
|
||||
* reaches the ceiling degrades to a slower page instead of a killed process.
|
||||
*/
|
||||
export function detectMemoryLimitMb(readFile = readFileSync) {
|
||||
const candidates = [
|
||||
"/sys/fs/cgroup/memory.max",
|
||||
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
|
||||
];
|
||||
for (const path of candidates) {
|
||||
let raw;
|
||||
try {
|
||||
raw = readFile(path, "utf8").trim();
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
// cgroup v1 reports "max" for an unlimited cgroup; v2 uses a bare
|
||||
// sentinel of a very large number on some kernels.
|
||||
if (raw === "max" || raw === "") continue;
|
||||
const bytes = Number(raw);
|
||||
if (!Number.isFinite(bytes) || bytes <= 0) continue;
|
||||
// A host-sized "limit" means no cgroup ceiling was applied.
|
||||
if (bytes >= Number.MAX_SAFE_INTEGER) continue;
|
||||
return heapLimitMb(bytes);
|
||||
}
|
||||
return heapLimitMb(Number.NaN);
|
||||
}
|
||||
|
||||
export function runtimeNodeOptions(
|
||||
existing = "",
|
||||
heapMb = detectMemoryLimitMb(),
|
||||
) {
|
||||
const flag = `--max-old-space-size=${heapMb}`;
|
||||
if (!existing.trim()) return flag;
|
||||
// Respect an explicit operator override; only add the cap when absent.
|
||||
if (existing.includes("--max-old-space-size")) return existing;
|
||||
return `${existing} ${flag}`;
|
||||
}
|
||||
|
||||
export function validateRuntime(settings) {
|
||||
const invalid = [];
|
||||
if (!settings.HOTEL_NAME?.trim() || settings.HOTEL_NAME === "Build fixture")
|
||||
@@ -23,22 +86,6 @@ export function validateRuntime(settings) {
|
||||
invalid.push(key);
|
||||
}
|
||||
}
|
||||
const localEnabled = ["true", "1"].includes(
|
||||
String(settings.CROWDSEC_LOCAL_ENABLED ?? "")
|
||||
.trim()
|
||||
.toLowerCase(),
|
||||
);
|
||||
if (localEnabled) {
|
||||
if (!settings.CROWDSEC_LAPI_API_KEY?.trim())
|
||||
invalid.push("CROWDSEC_LAPI_API_KEY");
|
||||
if (settings.CROWDSEC_LAPI_URL) {
|
||||
try {
|
||||
new URL(settings.CROWDSEC_LAPI_URL);
|
||||
} catch {
|
||||
invalid.push("CROWDSEC_LAPI_URL");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (invalid.length)
|
||||
throw new Error(`Invalid runtime configuration: ${invalid.join(", ")}`);
|
||||
}
|
||||
@@ -49,7 +96,13 @@ if (
|
||||
) {
|
||||
try {
|
||||
validateRuntime(process.env);
|
||||
const child = spawn(process.execPath, ["server.js"], { stdio: "inherit" });
|
||||
const heapMb = detectMemoryLimitMb();
|
||||
const nodeOptions = runtimeNodeOptions(process.env.NODE_OPTIONS, heapMb);
|
||||
console.log(`Starting CMS with a ${heapMb} MB V8 heap cap`);
|
||||
const child = spawn(process.execPath, ["server.js"], {
|
||||
stdio: "inherit",
|
||||
env: { ...process.env, NODE_OPTIONS: nodeOptions },
|
||||
});
|
||||
for (const signal of ["SIGTERM", "SIGINT"])
|
||||
process.on(signal, () => child.kill(signal));
|
||||
child.on("error", () => {
|
||||
|
||||
@@ -11,6 +11,9 @@ LOG_FILE="${LOG_FILE:-$DIR/logs/docker-update.log}"
|
||||
mkdir -p "$(dirname "$LOG_FILE")"
|
||||
log() { printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" | tee -a "$LOG_FILE"; }
|
||||
die() { log "ERROR: $*"; exit 1; }
|
||||
# A run that legitimately has nothing to do (the host is owned by CI) must not
|
||||
# look like a failure to cron. `die` stays reserved for real errors.
|
||||
skip() { log "SKIP: $*"; exit 0; }
|
||||
migration_image=""
|
||||
remote_migration_image=""
|
||||
previous_image=""
|
||||
@@ -58,10 +61,27 @@ trap 'exit 130' INT
|
||||
trap 'exit 143' TERM
|
||||
trap 'log "Update failed; inspect $LOG_FILE. No volumes or local files were deleted."' ERR
|
||||
|
||||
# An existing CI deployment is a different owner of the same host port.
|
||||
if [ "$(docker inspect --format '{{.State.Running}}' epicnext-cms-app 2>/dev/null || true)" = true ]; then
|
||||
die "This host is managed by CI (epicnext-cms-app). Update through CI, not a second Compose deployment."
|
||||
# This host belongs to CI: the blue/green deploy owns both host ports (3002 and
|
||||
# 3003) and one of the two slot containers is always the live release. Compose
|
||||
# may only run where CI does not.
|
||||
#
|
||||
# Checking epicnext-cms-app alone was not enough. After a cutover to the green
|
||||
# slot the blue container is stopped, renamed and deleted, so the guard stopped
|
||||
# firing while the host stayed CI-managed. `docker compose up` then recreated a
|
||||
# replica named epicnext-cms on port 3002 — the blue slot, exactly where the next
|
||||
# candidate has to start — and every later release failed on a busy port until
|
||||
# someone removed that container by hand (see logs/docker-update.cron.log).
|
||||
# Therefore: both slot containers count, and so does the nginx upstream, which is
|
||||
# the only thing that still marks the host as blue/green when a slot is idle.
|
||||
ci_upstream_file="${CMS_UPSTREAM_FILE:-/etc/nginx/snippets/cms_upstream_servers.conf}"
|
||||
if [ -r "$ci_upstream_file" ] && grep -qsE '127\.0\.0\.1:(3002|3003)' "$ci_upstream_file"; then
|
||||
skip "This host is managed by CI ($ci_upstream_file points at a blue/green slot). Update through CI, not a second Compose deployment."
|
||||
fi
|
||||
for slot_container in epicnext-cms-app epicnext-cms-green; do
|
||||
if [ "$(docker inspect --format '{{.State.Running}}' "$slot_container" 2>/dev/null || true)" = true ]; then
|
||||
skip "This host is managed by CI ($slot_container). Update through CI, not a second Compose deployment."
|
||||
fi
|
||||
done
|
||||
[[ -z "$(git status --porcelain --untracked-files=normal)" ]] || die "Working tree is not clean. Commit or stash local work first."
|
||||
if [[ "$UPDATE_SKIP_PULL" = 0 ]]; then
|
||||
git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}' >/dev/null || die "Configure this branch's Git upstream before updating."
|
||||
@@ -106,7 +126,7 @@ if [[ -n "${CMS_IMAGE_REPOSITORY:-}" ]]; then
|
||||
docker tag "$app_reference" "epicnext-cms:$CMS_RELEASE"
|
||||
docker tag "$remote_migration_image" "$migration_image"
|
||||
else
|
||||
docker build --network=host --target migrations --build-arg NEXT_DEPLOYMENT_ID="$CMS_RELEASE" -t "$migration_image" . >>"$LOG_FILE" 2>&1
|
||||
docker build --target migrations --build-arg NEXT_DEPLOYMENT_ID="$CMS_RELEASE" -t "$migration_image" . >>"$LOG_FILE" 2>&1
|
||||
docker compose build --build-arg NEXT_DEPLOYMENT_ID="$CMS_RELEASE" cms >>"$LOG_FILE" 2>&1
|
||||
fi
|
||||
expected_image="$(docker image inspect --format '{{.Id}}' "epicnext-cms:$CMS_RELEASE")"
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import "./load-env";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import { readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { CatalogItems, db, ItemsBase } from "@/lib/db";
|
||||
import { bundleExistsInDir } from "@/lib/furni/bundle-file";
|
||||
import { readFurniData } from "@/lib/services/furni-data";
|
||||
|
||||
const ICON_DIR = path.join(
|
||||
@@ -191,11 +192,10 @@ async function main(): Promise<void> {
|
||||
);
|
||||
|
||||
let nitroMissing = 0;
|
||||
const nitroSet = new Set(existsSync(NITRO_DIR) ? readdirSync(NITRO_DIR) : []);
|
||||
for (const cls of catalogClasses) {
|
||||
if (!nitroSet.has(`${cls}.nitro`)) nitroMissing++;
|
||||
if (!bundleExistsInDir(NITRO_DIR, cls)) nitroMissing++;
|
||||
}
|
||||
console.log(`[5] catalog items without .nitro bundle: ${nitroMissing}`);
|
||||
console.log(`[5] catalog items without a bundle on disk: ${nitroMissing}`);
|
||||
|
||||
await db.$client.end();
|
||||
process.exit(0);
|
||||
|
||||
@@ -0,0 +1,386 @@
|
||||
import "./load-env";
|
||||
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { db } from "@/lib/db";
|
||||
import {
|
||||
autoDetectInteraction,
|
||||
nitroAnimationStatesCount,
|
||||
} from "@/lib/furni/auto-interaction";
|
||||
import { parseNitroBundle } from "@/lib/services/swf/nitro-builder";
|
||||
|
||||
/**
|
||||
* Generate a plain SQL file that repairs every furniture row in `items_base`
|
||||
* from the visual logic inside each `.hab` / `.nitro` bundle:
|
||||
*
|
||||
* width / length / stack_height ← logic.model.dimensions x/y/z
|
||||
* allow_stack ← z > 0
|
||||
* allow_sit / allow_lay / allow_walk ← furnidata flags (cansiton/canlayon/canstandon)
|
||||
* interaction_modes_count ← real nitro animation states / mechanic
|
||||
* interaction_type ← logicType + classname mechanic (only fills 'default')
|
||||
*
|
||||
* The dimensions are NOT in furnidata — they live in the bundle JSON at
|
||||
* `logic.model.dimensions` (or top-level `dimensions` for official bundles).
|
||||
* The interaction columns mirror `verifyAndFixInteractionModesCount`
|
||||
* (src/lib/services/furni-import.ts:1341): real furnidata flags + .nitro states,
|
||||
* and `interaction_type` is only touched when it is still `default`.
|
||||
*
|
||||
* Usage:
|
||||
* pnpm exec tsx scripts/generate-stack-height-sql.ts [options]
|
||||
*
|
||||
* Options:
|
||||
* --dir=<path> bundle directory (default: /var/www/Gamedata/bundled/furniture)
|
||||
* --furnidata=<path> local FurnitureData.json (default: /var/www/Gamedata/config/FurnitureData.json)
|
||||
* --out=<path> SQL output file (default: stack-heights.sql)
|
||||
* --types=<list> items_base types (default: s,i → floor + wall furniture)
|
||||
* --all emit every matched row instead of only changed rows
|
||||
* --no-interaction dimensions/allow_stack only, skip interaction columns
|
||||
*
|
||||
* Nothing in the database is modified here; you run the generated file yourself.
|
||||
*/
|
||||
|
||||
const args = new Map<string, string | true>();
|
||||
for (const raw of process.argv.slice(2)) {
|
||||
const eq = raw.indexOf("=");
|
||||
if (eq === -1) args.set(raw.replace(/^--/, ""), true);
|
||||
else args.set(raw.slice(2, eq), raw.slice(eq + 1));
|
||||
}
|
||||
|
||||
const DIR = String(args.get("dir") ?? "/var/www/Gamedata/bundled/furniture");
|
||||
const FURNIDATA = String(
|
||||
args.get("furnidata") ?? "/var/www/Gamedata/config/FurnitureData.json",
|
||||
);
|
||||
const OUT = String(args.get("out") ?? "stack-heights.sql");
|
||||
const TYPES = String(args.get("types") ?? "s,i")
|
||||
.split(",")
|
||||
.map((t) => t.trim())
|
||||
.filter(Boolean);
|
||||
const EMIT_ALL = args.has("all");
|
||||
const WITH_INTERACTION = !args.has("no-interaction");
|
||||
|
||||
const EXTENSIONS = [".hab", ".nitro"] as const;
|
||||
|
||||
interface Dims {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
}
|
||||
|
||||
interface Flags {
|
||||
cansiton: boolean;
|
||||
canlayon: boolean;
|
||||
canstandon: boolean;
|
||||
}
|
||||
|
||||
interface BundleInfo {
|
||||
dims: Dims | null;
|
||||
animationStates: number | undefined;
|
||||
logicType: string | undefined;
|
||||
}
|
||||
|
||||
interface Row {
|
||||
item_name: string;
|
||||
public_name: string;
|
||||
width: number;
|
||||
length: number;
|
||||
stack_height: number;
|
||||
allow_stack: number | string;
|
||||
allow_sit: number | string;
|
||||
allow_lay: number | string;
|
||||
allow_walk: number | string;
|
||||
interaction_type: string;
|
||||
interaction_modes_count: number;
|
||||
}
|
||||
|
||||
/** Escape a value for a single-quoted MySQL string literal. */
|
||||
function q(value: string): string {
|
||||
return `'${value.replace(/\\/g, "\\\\").replace(/'/g, "''")}'`;
|
||||
}
|
||||
|
||||
/** `enum('0','1')` values must be quoted — an unquoted 0 is read as index 0 (''). */
|
||||
function qbit(value: boolean): string {
|
||||
return value ? "'1'" : "'0'";
|
||||
}
|
||||
|
||||
function isTruthyBit(value: number | string): boolean {
|
||||
return Number(value) === 1;
|
||||
}
|
||||
|
||||
/** Pull dimensions out of a parsed bundle JSON, tolerating both layouts. */
|
||||
function extractDims(json: unknown): Dims | null {
|
||||
const root = json as Record<string, unknown> | null;
|
||||
if (!root) return null;
|
||||
const logic = root.logic as Record<string, unknown> | undefined;
|
||||
const model = logic?.model as Record<string, unknown> | undefined;
|
||||
const candidate =
|
||||
(model?.dimensions as Dims | undefined) ??
|
||||
(logic?.dimensions as Dims | undefined) ??
|
||||
(root.dimensions as Dims | undefined);
|
||||
if (!candidate) return null;
|
||||
const x = Number(candidate.x);
|
||||
const y = Number(candidate.y);
|
||||
const z = Number(candidate.z);
|
||||
if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(z)) {
|
||||
return null;
|
||||
}
|
||||
return { x, y, z };
|
||||
}
|
||||
|
||||
/** Parse a bundle once and cache everything we need from it. */
|
||||
function readBundleInfo(path: string): BundleInfo {
|
||||
try {
|
||||
const json = parseNitroBundle(readFileSync(path)).json as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
return {
|
||||
dims: extractDims(json),
|
||||
animationStates: nitroAnimationStatesCount(json),
|
||||
logicType:
|
||||
typeof json.logicType === "string" ? json.logicType : undefined,
|
||||
};
|
||||
} catch {
|
||||
return { dims: null, animationStates: undefined, logicType: undefined };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the local FurnitureData.json into a classname → flags map. Duplicate
|
||||
* classnames are merged so a true flag in any entry wins (same rule as the
|
||||
* app's `lookupRealFurniFlags`).
|
||||
*/
|
||||
function loadFurniFlags(path: string): Map<string, Flags> {
|
||||
const map = new Map<string, Flags>();
|
||||
if (!existsSync(path)) return map;
|
||||
try {
|
||||
const json = JSON.parse(readFileSync(path, "utf-8")) as Record<
|
||||
string,
|
||||
{ furnitype?: Array<Record<string, unknown>> }
|
||||
>;
|
||||
for (const section of ["roomitemtypes", "wallitemtypes"] as const) {
|
||||
for (const item of json[section]?.furnitype ?? []) {
|
||||
const cn = item.classname;
|
||||
if (typeof cn !== "string" || !cn) continue;
|
||||
const candidate: Flags = {
|
||||
cansiton: !!item.cansiton,
|
||||
canlayon: !!item.canlayon,
|
||||
canstandon: !!item.canstandon,
|
||||
};
|
||||
const existing = map.get(cn);
|
||||
if (!existing) {
|
||||
map.set(cn, candidate);
|
||||
continue;
|
||||
}
|
||||
existing.cansiton = existing.cansiton || candidate.cansiton;
|
||||
existing.canlayon = existing.canlayon || candidate.canlayon;
|
||||
existing.canstandon = existing.canstandon || candidate.canstandon;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// unreadable furnidata → interaction columns are skipped
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const t0 = Date.now();
|
||||
const dir = resolve(DIR);
|
||||
const entries = new Set(readdirSync(dir));
|
||||
const infoCache = new Map<string, BundleInfo>();
|
||||
const flagsMap = WITH_INTERACTION
|
||||
? loadFurniFlags(resolve(FURNIDATA))
|
||||
: new Map<string, Flags>();
|
||||
|
||||
const [rows] = (await db.execute(
|
||||
sql`SELECT item_name, public_name, width, length, stack_height, allow_stack,
|
||||
allow_sit, allow_lay, allow_walk, interaction_type, interaction_modes_count
|
||||
FROM items_base
|
||||
WHERE type IN (${sql.join(
|
||||
TYPES.map((t) => sql`${t}`),
|
||||
sql`, `,
|
||||
)})`,
|
||||
)) as unknown as [Row[], unknown];
|
||||
|
||||
console.log(
|
||||
`[stack-sql] ${rows.length} furniture rows | ${entries.size} bundles | ${flagsMap.size} furnidata flags`,
|
||||
);
|
||||
|
||||
const updates: string[] = [];
|
||||
let matched = 0;
|
||||
let changed = 0;
|
||||
let resolvedBase = 0;
|
||||
const counts = {
|
||||
dims: 0,
|
||||
allowFlags: 0,
|
||||
modes: 0,
|
||||
interactionType: 0,
|
||||
};
|
||||
const missing: string[] = [];
|
||||
const unparsed: string[] = [];
|
||||
|
||||
for (const row of rows) {
|
||||
const name = row.item_name;
|
||||
const base = name.includes("*") ? name.slice(0, name.indexOf("*")) : name;
|
||||
const variants = [name, name.trim(), base, base.trim()];
|
||||
|
||||
let chosen: string | null = null;
|
||||
for (const variant of variants) {
|
||||
for (const ext of EXTENSIONS) {
|
||||
const candidate = `${variant}${ext}`;
|
||||
if (entries.has(candidate)) {
|
||||
chosen = candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (chosen) break;
|
||||
}
|
||||
if (!chosen) {
|
||||
missing.push(name);
|
||||
continue;
|
||||
}
|
||||
if (!chosen.startsWith(`${name}.`)) resolvedBase++;
|
||||
|
||||
let info = infoCache.get(chosen);
|
||||
if (!info) {
|
||||
info = readBundleInfo(resolve(dir, chosen));
|
||||
infoCache.set(chosen, info);
|
||||
}
|
||||
if (!info.dims) {
|
||||
unparsed.push(`${name} (${chosen})`);
|
||||
continue;
|
||||
}
|
||||
matched++;
|
||||
|
||||
const width = Math.trunc(info.dims.x);
|
||||
const length = Math.trunc(info.dims.y);
|
||||
const stackHeight = Number(info.dims.z.toFixed(2));
|
||||
const allowStack = info.dims.z > 0;
|
||||
|
||||
const setParts: string[] = [];
|
||||
const dimsChanged =
|
||||
row.width !== width ||
|
||||
row.length !== length ||
|
||||
Number(row.stack_height) !== stackHeight ||
|
||||
isTruthyBit(row.allow_stack) !== allowStack;
|
||||
if (dimsChanged) counts.dims++;
|
||||
setParts.push(
|
||||
`width=${width}`,
|
||||
`length=${length}`,
|
||||
`stack_height=${stackHeight}`,
|
||||
`allow_stack=${qbit(allowStack)}`,
|
||||
);
|
||||
|
||||
// Interaction columns — only when the classname exists in furnidata
|
||||
// (mirrors the app sweep: authoritative flags, never keyword guesses).
|
||||
const flags =
|
||||
flagsMap.get(name) ??
|
||||
flagsMap.get(base) ??
|
||||
flagsMap.get(name.trim()) ??
|
||||
flagsMap.get(base.trim());
|
||||
if (WITH_INTERACTION && flags) {
|
||||
const auto = autoDetectInteraction(name, row.public_name || undefined, {
|
||||
cansiton: flags.cansiton,
|
||||
canlayon: flags.canlayon,
|
||||
canstandon: flags.canstandon,
|
||||
hasActionData: true,
|
||||
animationStates: info.animationStates,
|
||||
logicType: info.logicType,
|
||||
});
|
||||
|
||||
if (
|
||||
Number(row.allow_sit) !== 2 &&
|
||||
isTruthyBit(row.allow_sit) !== auto.canSit
|
||||
) {
|
||||
setParts.push(`allow_sit=${qbit(auto.canSit)}`);
|
||||
counts.allowFlags++;
|
||||
}
|
||||
if (
|
||||
Number(row.allow_lay) !== 2 &&
|
||||
isTruthyBit(row.allow_lay) !== auto.canLay
|
||||
) {
|
||||
setParts.push(`allow_lay=${qbit(auto.canLay)}`);
|
||||
counts.allowFlags++;
|
||||
}
|
||||
if (
|
||||
Number(row.allow_walk) !== 2 &&
|
||||
isTruthyBit(row.allow_walk) !== auto.canStand
|
||||
) {
|
||||
setParts.push(`allow_walk=${qbit(auto.canStand)}`);
|
||||
counts.allowFlags++;
|
||||
}
|
||||
if (
|
||||
Number(row.interaction_modes_count ?? 0) !== auto.interactionModesCount
|
||||
) {
|
||||
setParts.push(`interaction_modes_count=${auto.interactionModesCount}`);
|
||||
counts.modes++;
|
||||
}
|
||||
if (
|
||||
(row.interaction_type === "default" || !row.interaction_type) &&
|
||||
auto.interactionType !== "default"
|
||||
) {
|
||||
setParts.push(`interaction_type=${q(auto.interactionType)}`);
|
||||
counts.interactionType++;
|
||||
}
|
||||
}
|
||||
|
||||
const isChanged =
|
||||
dimsChanged ||
|
||||
setParts.some(
|
||||
(p) =>
|
||||
!p.startsWith("width=") &&
|
||||
!p.startsWith("length=") &&
|
||||
!p.startsWith("stack_height=") &&
|
||||
!p.startsWith("allow_stack="),
|
||||
);
|
||||
if (isChanged) changed++;
|
||||
if (!EMIT_ALL && !isChanged) continue;
|
||||
|
||||
updates.push(
|
||||
`UPDATE items_base SET ${setParts.join(", ")} WHERE item_name=${q(name)} AND type IN (${TYPES.map(q).join(", ")});`,
|
||||
);
|
||||
}
|
||||
|
||||
const header = [
|
||||
"-- Auto-generated by scripts/generate-stack-height-sql.ts",
|
||||
`-- Source bundles: ${dir}`,
|
||||
`-- Furnidata: ${WITH_INTERACTION ? resolve(FURNIDATA) : "(disabled)"}`,
|
||||
`-- Generated: ${new Date().toISOString()}`,
|
||||
`-- Furniture rows: ${rows.length} | matched: ${matched} | changed: ${changed} | missing bundle: ${missing.length} | unparsable: ${unparsed.length}`,
|
||||
`-- Changes: dimensions ${counts.dims} | allow_sit/lay/walk ${counts.allowFlags} | modes_count ${counts.modes} | interaction_type ${counts.interactionType}`,
|
||||
`-- Mode: ${EMIT_ALL ? "all matched rows" : "changed rows only"}`,
|
||||
"",
|
||||
"START TRANSACTION;",
|
||||
"",
|
||||
].join("\n");
|
||||
const footer = "\n\nCOMMIT;\n";
|
||||
|
||||
writeFileSync(
|
||||
resolve(OUT),
|
||||
`${header}${updates.join("\n")}${footer}`,
|
||||
"utf-8",
|
||||
);
|
||||
|
||||
console.log(`[stack-sql] matched ${matched}, changed ${changed}`);
|
||||
console.log(
|
||||
`[stack-sql] changes -> dims ${counts.dims}, allow flags ${counts.allowFlags}, modes ${counts.modes}, interaction_type ${counts.interactionType}`,
|
||||
);
|
||||
if (resolvedBase > 0)
|
||||
console.log(`[stack-sql] resolved via base classname: ${resolvedBase}`);
|
||||
console.log(`[stack-sql] updates written: ${updates.length}`);
|
||||
if (missing.length) {
|
||||
console.log(
|
||||
`[stack-sql] no bundle (${missing.length}): ${missing.slice(0, 20).join(", ")}${missing.length > 20 ? ", …" : ""}`,
|
||||
);
|
||||
}
|
||||
if (unparsed.length) {
|
||||
console.log(
|
||||
`[stack-sql] unparsable (${unparsed.length}): ${unparsed.slice(0, 20).join(", ")}${unparsed.length > 20 ? ", …" : ""}`,
|
||||
);
|
||||
}
|
||||
console.log(`[stack-sql] wrote ${resolve(OUT)} in ${Date.now() - t0}ms`);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
Executable
+72
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env bash
|
||||
# Alert when the local avatar imager stops rendering, and again when it recovers.
|
||||
#
|
||||
# Intended to run from cron every few minutes (see `crontab -l`). Steady-state is
|
||||
# silent; a state change posts to DISCORD_WEBHOOK_URL (from the environment or
|
||||
# .env). While the imager stays down it re-alerts at most once per
|
||||
# HEALTH_ALERT_COOLDOWN_MIN. The script always exits 0 so an outage never turns
|
||||
# into a noisy cron failure; the alert itself (and this log) is the signal.
|
||||
set -Eeuo pipefail
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
LOG_FILE="${IMAGER_HEALTH_LOG:-$DIR/logs/imager-health.log}"
|
||||
STATE_FILE="${IMAGER_HEALTH_STATE:-$DIR/logs/imager-health.state}"
|
||||
HEALTH_URL="${IMAGER_HEALTH_URL:-http://127.0.0.1:8082/health}"
|
||||
mkdir -p "$(dirname "$LOG_FILE")" "$(dirname "$STATE_FILE")"
|
||||
log() { printf '[%s] %s\n' "$(date '+%F %T')" "$*" | tee -a "$LOG_FILE"; }
|
||||
|
||||
load_env() {
|
||||
local name="$1"
|
||||
if [ -n "${!name:-}" ]; then return 0; fi
|
||||
if [ -f "$DIR/.env" ]; then
|
||||
local line
|
||||
line="$(grep -m1 "^$name=" "$DIR/.env" | cut -d= -f2- | tr -d "'\"")" || true
|
||||
if [ -n "$line" ]; then printf -v "$name" '%s' "$line"; fi
|
||||
fi
|
||||
}
|
||||
|
||||
load_env DISCORD_WEBHOOK_URL || true
|
||||
COOLDOWN_MIN="${HEALTH_ALERT_COOLDOWN_MIN:-30}"
|
||||
|
||||
notify() {
|
||||
local msg="$1"
|
||||
log "ALERT: $msg"
|
||||
if [ -z "${DISCORD_WEBHOOK_URL:-}" ]; then
|
||||
log "no DISCORD_WEBHOOK_URL configured; alert written to log only"
|
||||
return 0
|
||||
fi
|
||||
local payload
|
||||
payload="$(python3 -c 'import json,sys;print(json.dumps({"content":sys.argv[1]}))' "$msg")"
|
||||
curl -sS -m 10 -H "Content-Type: application/json" -d "$payload" \
|
||||
"$DISCORD_WEBHOOK_URL" >/dev/null 2>&1 || log "failed to post alert to Discord"
|
||||
}
|
||||
|
||||
body="$(curl -sS -m 5 "$HEALTH_URL" 2>/dev/null || true)"
|
||||
ready=0
|
||||
if printf '%s' "$body" | grep -q '"ready":true'; then ready=1; fi
|
||||
|
||||
now="$(date +%s)"
|
||||
prev_state="up"
|
||||
prev_at=0
|
||||
if [ -f "$STATE_FILE" ]; then read -r prev_state prev_at < "$STATE_FILE" || true; fi
|
||||
[ -n "${prev_state:-}" ] || prev_state=up
|
||||
[ -n "${prev_at:-}" ] || prev_at=0
|
||||
|
||||
if [ "$ready" = 1 ]; then
|
||||
if [ "$prev_state" != "up" ]; then
|
||||
notify "Avatar imager RECOVERED ($HEALTH_URL reports ready)"
|
||||
fi
|
||||
printf 'up %s\n' "$now" > "$STATE_FILE"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ "$prev_state" != "down" ]; then
|
||||
notify "Avatar imager DOWN ($HEALTH_URL not ready: ${body:-unreachable})"
|
||||
printf 'down %s\n' "$now" > "$STATE_FILE"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ "$(( now - prev_at ))" -ge "$(( COOLDOWN_MIN * 60 ))" ]; then
|
||||
notify "Avatar imager STILL DOWN after ${COOLDOWN_MIN}m ($HEALTH_URL)"
|
||||
printf 'down %s\n' "$now" > "$STATE_FILE"
|
||||
fi
|
||||
exit 0
|
||||
+71
-5
@@ -1,9 +1,17 @@
|
||||
import { drainOperationEffects } from "../src/features/operations/worker";
|
||||
import { drainFurnitureImports } from "../src/lib/services/furni-job-worker";
|
||||
// Must stay the first import. ESM evaluates a module's imports in source
|
||||
// order, and `../src/features/operations/worker` reaches `@/env`, which parses
|
||||
// process.env at import time. With this import further down the tree, load-env
|
||||
// ran *after* the schema validation had already thrown on a missing
|
||||
// DATABASE_URL, so the worker could only ever start from an environment that
|
||||
// already exported the config — which is why `pnpm jobs:worker` died
|
||||
// immediately and nothing supervised it.
|
||||
import "./load-env";
|
||||
import * as nodeFs from "node:fs";
|
||||
import * as nodePath from "node:path";
|
||||
import { Cron } from "croner";
|
||||
import { lt, sql } from "drizzle-orm";
|
||||
import { env } from "../src/env";
|
||||
import { drainOperationEffects } from "../src/features/operations/worker";
|
||||
import { db, PasswordReset, WebsiteLoginLogs } from "../src/lib/db";
|
||||
import { logger } from "../src/lib/logger";
|
||||
import { redis } from "../src/lib/redis";
|
||||
@@ -18,6 +26,7 @@ import {
|
||||
diskLevel,
|
||||
parseDfOutput,
|
||||
} from "../src/lib/services/disk-usage";
|
||||
import { drainFurnitureImports } from "../src/lib/services/furni-job-worker";
|
||||
import { publishDueArticles } from "../src/lib/services/news-scheduler";
|
||||
import { scheduledAutoCleanFakeNitros } from "../src/lib/services/nitro-cleanup";
|
||||
import { rcon } from "../src/lib/services/rcon";
|
||||
@@ -161,13 +170,69 @@ async function checkDiskUsage(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the JAR to back up. `EMULATOR_JAR_PATH` may point at the file itself
|
||||
* or at a directory of release JARs, because the emulator's own unit file
|
||||
* launches `ls -t Polaris-*-jar-with-dependencies.jar` — a path pinned to one
|
||||
* release filename goes stale on the next emulator upgrade, and a stale path
|
||||
* fails as a bare ENOENT from copyFile that gives no hint what is wrong. A
|
||||
* directory (or a path with a `*`) resolves to the most recently modified JAR,
|
||||
* matching how the emulator actually picks its build.
|
||||
*/
|
||||
export function resolveEmulatorJar(
|
||||
configuredPath: string,
|
||||
fs: typeof import("node:fs") = nodeFs,
|
||||
{ resolve }: typeof import("node:path") = nodePath,
|
||||
): string | null {
|
||||
const { existsSync, readdirSync, statSync } = fs;
|
||||
if (configuredPath.includes("*")) {
|
||||
const dir = configuredPath.slice(0, configuredPath.lastIndexOf("/") + 1);
|
||||
const pattern = configuredPath.slice(dir.length);
|
||||
if (!existsSync(dir)) return null;
|
||||
return (
|
||||
readdirSync(dir)
|
||||
.filter((name: string) => name.startsWith(pattern.split("*")[0] ?? ""))
|
||||
.map((name: string) => resolve(dir, name))
|
||||
.filter((path: string) => existsSync(path))
|
||||
.sort(
|
||||
(a: string, b: string) => statSync(b).mtimeMs - statSync(a).mtimeMs,
|
||||
)[0] ?? null
|
||||
);
|
||||
}
|
||||
if (existsSync(configuredPath) && statSync(configuredPath).isFile())
|
||||
return configuredPath;
|
||||
// A directory: take the newest JAR in it.
|
||||
if (existsSync(configuredPath) && statSync(configuredPath).isDirectory()) {
|
||||
return (
|
||||
readdirSync(configuredPath)
|
||||
.filter((name: string) => name.endsWith(".jar"))
|
||||
.map((name: string) => resolve(configuredPath, name))
|
||||
.sort(
|
||||
(a: string, b: string) => statSync(b).mtimeMs - statSync(a).mtimeMs,
|
||||
)[0] ?? null
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function backupEmulatorJar(): Promise<void> {
|
||||
if (!env.EMULATOR_JAR_PATH || !env.EMULATOR_BACKUP_DIR) return;
|
||||
|
||||
const { copyFileSync, mkdirSync, readdirSync, unlinkSync, existsSync } =
|
||||
await import("node:fs");
|
||||
const fs = await import("node:fs");
|
||||
const { copyFileSync, mkdirSync, readdirSync, unlinkSync, existsSync } = fs;
|
||||
const { resolve } = await import("node:path");
|
||||
|
||||
const jarPath = resolveEmulatorJar(env.EMULATOR_JAR_PATH, fs, nodePath);
|
||||
if (!jarPath) {
|
||||
// Configured but unusable: say so once, loudly, instead of every night
|
||||
// logging an opaque copyFile ENOENT that reads like a permissions bug.
|
||||
logger.error(
|
||||
"Emulator JAR backup skipped: EMULATOR_JAR_PATH does not resolve to a JAR",
|
||||
{ module: "jobs", configured: env.EMULATOR_JAR_PATH },
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const timestamp = new Date().toISOString().slice(0, 19).replace(/[T:]/g, "-");
|
||||
const backupFile = resolve(
|
||||
env.EMULATOR_BACKUP_DIR,
|
||||
@@ -179,10 +244,11 @@ async function backupEmulatorJar(): Promise<void> {
|
||||
}
|
||||
|
||||
try {
|
||||
copyFileSync(env.EMULATOR_JAR_PATH, backupFile);
|
||||
copyFileSync(jarPath, backupFile);
|
||||
logger.info("Backed up emulator JAR", {
|
||||
module: "jobs",
|
||||
backupFile,
|
||||
source: jarPath,
|
||||
});
|
||||
|
||||
const keep = env.EMULATOR_BACKUP_KEEP ?? 7;
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
#!/usr/bin/env tsx
|
||||
/**
|
||||
* Rename on-disk furniture bundles from `.nitro` to `.hab`.
|
||||
*
|
||||
* Imports have written `<classname>.hab` since the bundle-extension switch, but
|
||||
* everything already on disk kept its old name. That matters at runtime: the
|
||||
* client asks for `.hab`, so a catalogue whose assets are still `.nitro` shows
|
||||
* furniture that renders as nothing. This script closes that gap.
|
||||
*
|
||||
* It is deliberately conservative:
|
||||
* - refuses to run without `--dry-run` or `--yes`;
|
||||
* - never overwrites an existing `.hab` — a conflict is reported, not resolved;
|
||||
* - never deletes anything, so a half-finished run is recoverable by hand;
|
||||
* - idempotent: a second run over migrated dirs is a no-op.
|
||||
*
|
||||
* Run it in a maintenance window. The rename is per-file, so a client request
|
||||
* for a given `.nitro` 404s from the moment that file is renamed until the
|
||||
* client asks for `.hab`.
|
||||
*
|
||||
* Usage:
|
||||
* pnpm tsx scripts/migrate-nitro-to-hab.ts --dry-run
|
||||
* pnpm tsx scripts/migrate-nitro-to-hab.ts --yes
|
||||
* pnpm tsx scripts/migrate-nitro-to-hab.ts --yes --dir /var/www/extra/bundles
|
||||
* pnpm tsx scripts/migrate-nitro-to-hab.ts --yes --skip-generic
|
||||
*/
|
||||
|
||||
import "./load-env";
|
||||
import { existsSync, promises as fs } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { db } from "@/lib/db";
|
||||
import {
|
||||
getFurniAssetWriteTargets,
|
||||
getGamedataRoot,
|
||||
} from "@/lib/services/furni-asset-dirs";
|
||||
import { getPublicAssetRoot } from "@/lib/services/public-asset-root";
|
||||
import { siteSettings } from "@/lib/services/site-settings";
|
||||
import { getRuntimePath } from "@/lib/utils/runtime-path";
|
||||
|
||||
const LEGACY_EXT = ".nitro";
|
||||
const TARGET_EXT = ".hab";
|
||||
|
||||
interface Args {
|
||||
dryRun: boolean;
|
||||
yes: boolean;
|
||||
skipGeneric: boolean;
|
||||
dirs: string[];
|
||||
}
|
||||
|
||||
function parseArgs(argv: string[]): Args {
|
||||
const dirs: string[] = [];
|
||||
let dryRun = false;
|
||||
let yes = false;
|
||||
let skipGeneric = false;
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const arg = argv[i];
|
||||
if (arg === "--dry-run") dryRun = true;
|
||||
else if (arg === "--yes" || arg === "-y") yes = true;
|
||||
else if (arg === "--skip-generic") skipGeneric = true;
|
||||
else if (arg === "--dir") {
|
||||
const value = argv[++i];
|
||||
if (!value) throw Error("--dir needs a path");
|
||||
dirs.push(path.resolve(value));
|
||||
} else throw Error(`Unknown argument: ${arg}`);
|
||||
}
|
||||
return { dryRun, yes, skipGeneric, dirs };
|
||||
}
|
||||
|
||||
/**
|
||||
* Where the app keeps bundles. Mirrors the resolution in figure-import.ts /
|
||||
* effect-import.ts / pet-import.ts so the script follows the same site settings
|
||||
* the running instance uses. Settings are best-effort: a database that is down
|
||||
* must not stop an operator from migrating files, so failures fall back to the
|
||||
* on-disk defaults rather than aborting.
|
||||
*/
|
||||
async function resolveDirs(skipGeneric: boolean): Promise<string[]> {
|
||||
const found: string[] = [];
|
||||
const push = (dir: string | null | undefined) => {
|
||||
if (dir?.trim()) found.push(path.resolve(dir.trim()));
|
||||
};
|
||||
|
||||
// Furniture: primary + every configured mirror (gamedata, nitro-files).
|
||||
try {
|
||||
const targets = await getFurniAssetWriteTargets();
|
||||
push(targets.nitroDir);
|
||||
for (const mirror of targets.mirrorDirs) push(mirror.nitroDir);
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
` ! could not read furniture asset settings (${(error as Error).message}); using defaults`,
|
||||
);
|
||||
push(
|
||||
getRuntimePath(process.cwd(), "public/nitro-assets/bundled/furniture"),
|
||||
);
|
||||
push("/var/www/Gamedata/bundled/furniture");
|
||||
}
|
||||
|
||||
let gamedataRoot = "";
|
||||
try {
|
||||
gamedataRoot = await getGamedataRoot();
|
||||
} catch {
|
||||
/* defaults below cover it */
|
||||
}
|
||||
|
||||
for (const type of ["figure", "effect"]) {
|
||||
let configured = "";
|
||||
try {
|
||||
configured = (
|
||||
(await siteSettings.get(`${type}_nitro_dir`, "")) ?? ""
|
||||
).trim();
|
||||
} catch {
|
||||
/* fall through to defaults */
|
||||
}
|
||||
if (configured) push(configured);
|
||||
else if (gamedataRoot)
|
||||
push(getRuntimePath(gamedataRoot, `bundled/${type}`));
|
||||
else
|
||||
push(
|
||||
getRuntimePath(
|
||||
getPublicAssetRoot(),
|
||||
`public/nitro-assets/bundled/${type}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Pets: the CMS dir is `pet`, this deployment's gamedata dir is `pets`.
|
||||
// Both spellings are listed so neither is silently skipped.
|
||||
push(getRuntimePath(getPublicAssetRoot(), "public/nitro-assets/bundled/pet"));
|
||||
if (gamedataRoot) {
|
||||
push(getRuntimePath(gamedataRoot, "bundled/pet"));
|
||||
push(getRuntimePath(gamedataRoot, "bundled/pets"));
|
||||
}
|
||||
|
||||
// `generic` holds the stock client UI bundles (selection_arrow, room,
|
||||
// tile_cursor, place_holder…) that this CMS never imported. They are included
|
||||
// by default because the renderer's `generic.asset.url` template resolves to
|
||||
// `.hab` too — leaving them behind breaks the room view, not just furniture.
|
||||
if (!skipGeneric && gamedataRoot) {
|
||||
push(getRuntimePath(gamedataRoot, "bundled/generic"));
|
||||
}
|
||||
|
||||
return [...new Set(found)];
|
||||
}
|
||||
|
||||
interface DirResult {
|
||||
dir: string;
|
||||
renamed: number;
|
||||
alreadyHab: number;
|
||||
conflicts: Array<{ from: string; to: string }>;
|
||||
errors: Array<{ file: string; message: string }>;
|
||||
}
|
||||
|
||||
async function migrateDir(dir: string, dryRun: boolean): Promise<DirResult> {
|
||||
const result: DirResult = {
|
||||
dir,
|
||||
renamed: 0,
|
||||
alreadyHab: 0,
|
||||
conflicts: [],
|
||||
errors: [],
|
||||
};
|
||||
|
||||
let entries: string[];
|
||||
try {
|
||||
entries = await fs.readdir(dir);
|
||||
} catch (error) {
|
||||
result.errors.push({ file: dir, message: (error as Error).message });
|
||||
return result;
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
if (!entry.toLowerCase().endsWith(LEGACY_EXT)) continue;
|
||||
const from = path.join(dir, entry);
|
||||
const to = path.join(
|
||||
dir,
|
||||
`${entry.slice(0, -LEGACY_EXT.length)}${TARGET_EXT}`,
|
||||
);
|
||||
|
||||
// Never clobber. A pre-existing `.hab` is a live bundle the client is
|
||||
// already serving; leaving the `.nitro` alone is the only safe answer.
|
||||
if (existsSync(to)) {
|
||||
result.conflicts.push({
|
||||
from: path.basename(from),
|
||||
to: path.basename(to),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dryRun) {
|
||||
result.renamed++;
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
await fs.rename(from, to);
|
||||
result.renamed++;
|
||||
} catch (error) {
|
||||
result.errors.push({ file: entry, message: (error as Error).message });
|
||||
}
|
||||
}
|
||||
|
||||
// Report the target state too, so a run confirms the end condition rather
|
||||
// than just the work it did.
|
||||
for (const entry of await fs.readdir(dir)) {
|
||||
if (entry.toLowerCase().endsWith(TARGET_EXT)) result.alreadyHab++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
|
||||
if (!args.dryRun && !args.yes) {
|
||||
console.error(
|
||||
"Nothing to do: pass --dry-run to preview, or --yes to rename for real.",
|
||||
);
|
||||
process.exitCode = 2;
|
||||
}
|
||||
|
||||
const dirs = [
|
||||
...new Set([...args.dirs, ...(await resolveDirs(args.skipGeneric))]),
|
||||
];
|
||||
const existing = dirs.filter((dir) => existsSync(dir));
|
||||
|
||||
console.log(
|
||||
args.dryRun
|
||||
? "DRY RUN — no files will be touched."
|
||||
: "Renaming .nitro bundles to .hab.",
|
||||
);
|
||||
if (!args.dryRun) {
|
||||
console.log(
|
||||
"Run this in a maintenance window: the client 404s on each file between its rename and its switch to .hab.",
|
||||
);
|
||||
}
|
||||
console.log(`\nDirectories (${existing.length} of ${dirs.length} exist):`);
|
||||
for (const dir of existing) console.log(` ${dir}`);
|
||||
const missing = dirs.filter((dir) => !existsSync(dir));
|
||||
if (missing.length) {
|
||||
console.log(`\nNot present, skipped:`);
|
||||
for (const dir of missing) console.log(` ${dir}`);
|
||||
}
|
||||
if (!existing.length) {
|
||||
console.log("\nNo bundle directories found — nothing to migrate.");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("");
|
||||
const results: DirResult[] = [];
|
||||
for (const dir of existing) {
|
||||
results.push(await migrateDir(dir, args.dryRun));
|
||||
}
|
||||
|
||||
let totalRenamed = 0;
|
||||
let totalHab = 0;
|
||||
let totalConflicts = 0;
|
||||
let totalErrors = 0;
|
||||
for (const result of results) {
|
||||
totalRenamed += result.renamed;
|
||||
totalHab += result.alreadyHab;
|
||||
totalConflicts += result.conflicts.length;
|
||||
totalErrors += result.errors.length;
|
||||
console.log(
|
||||
`${result.dir}\n` +
|
||||
` ${args.dryRun ? "would rename" : "renamed"}: ${result.renamed}\n` +
|
||||
` .hab present: ${result.alreadyHab}`,
|
||||
);
|
||||
for (const conflict of result.conflicts) {
|
||||
console.log(
|
||||
` CONFLICT: ${conflict.from} — ${conflict.to} already exists`,
|
||||
);
|
||||
}
|
||||
for (const error of result.errors) {
|
||||
console.log(` ERROR: ${error.file} — ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(
|
||||
`\n${args.dryRun ? "Would rename" : "Renamed"} ${totalRenamed} file(s). ` +
|
||||
`${totalHab} .hab bundle(s) present afterwards.`,
|
||||
);
|
||||
if (totalConflicts) {
|
||||
console.log(
|
||||
`\n${totalConflicts} conflict(s): a .hab with that name already exists. ` +
|
||||
"The .nitro was left in place. Resolve these by hand — the client can only load one of the two.",
|
||||
);
|
||||
}
|
||||
if (totalErrors)
|
||||
console.log(`\n${totalErrors} error(s); re-run once they are fixed.`);
|
||||
|
||||
// Non-zero on a partial migration so a wrapper cannot report success.
|
||||
if (totalConflicts || totalErrors) process.exitCode = 1;
|
||||
}
|
||||
|
||||
// The settings lookups open a mysql2 pool, which keeps the event loop alive —
|
||||
// the script prints its whole report and then sits there burning a timeout
|
||||
// instead of exiting. Closing the pool is not enough on its own here, so the
|
||||
// exit is explicit, matching scripts/furni-diagnose-now.ts.
|
||||
main()
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
})
|
||||
.then(async () => {
|
||||
await db.$client.end().catch(() => {});
|
||||
process.exit(process.exitCode ?? 0);
|
||||
});
|
||||
@@ -101,6 +101,15 @@ fi
|
||||
if [[ ! -d /var/log/nginx ]]; then
|
||||
install -d -o root -g adm -m 750 /var/log/nginx
|
||||
fi
|
||||
# nginx-cms.conf sets `root /var/www/html` so that disk-backed locations
|
||||
# (favicon.ico) resolve somewhere the www-data worker can actually traverse.
|
||||
# The previous implicit root was /etc/nginx/html, which sits behind /etc/nginx
|
||||
# (0750 root:root): the worker got EACCES on every stat, and nginx logs a
|
||||
# failed stat at crit, so each crawler probe wrote a crit line.
|
||||
if [[ ! -d /var/www/html ]]; then
|
||||
install -d -o root -g root -m 755 /var/www/html
|
||||
echo "+ created /var/www/html (document root)"
|
||||
fi
|
||||
for f in /var/log/nginx/access.log /var/log/nginx/error.log; do
|
||||
[[ -f "$f" ]] || touch "$f"
|
||||
done
|
||||
|
||||
@@ -79,6 +79,27 @@ function filesFrom(values) {
|
||||
return values.map(normalizeAsset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Webpack interleaves numeric chunk ids with file names in `chunks` arrays, so
|
||||
* a real file has to be separated from its id. An id is rejected by
|
||||
* normalizeAsset for the right reason (it has no `static/` prefix and no `.js`
|
||||
* suffix), which makes it a usable filter — but only for ids. A malformed
|
||||
* *path* must still fail loudly rather than be silently dropped, or a broken
|
||||
* manifest would quietly under-report a route's real weight.
|
||||
*/
|
||||
function assetFilesFromChunkList(values) {
|
||||
if (!Array.isArray(values))
|
||||
throw new Error("Unsupported JavaScript chunk list.");
|
||||
const files = [];
|
||||
for (const value of values) {
|
||||
if (typeof value !== "string")
|
||||
throw new Error("Non-string JavaScript asset.");
|
||||
if (/^\d+$/.test(value)) continue;
|
||||
files.push(normalizeAsset(value));
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
export function measureRoute({
|
||||
budget,
|
||||
appPath,
|
||||
@@ -86,18 +107,44 @@ export function measureRoute({
|
||||
clientManifest,
|
||||
readAsset,
|
||||
}) {
|
||||
// Turbopack emits an explicit per-segment `entryJSFiles` list. Webpack does
|
||||
// not — it only records chunks per client module — so after the build moved
|
||||
// to webpack (3d828a61) every route reported "unavailable" and the report
|
||||
// silently stopped measuring anything. Fall back to the same source Next's
|
||||
// own `static-routes-info` uses for webpack builds.
|
||||
const entries = clientManifest?.entryJSFiles;
|
||||
if (!entries || typeof entries !== "object" || Array.isArray(entries))
|
||||
const webpackModules = clientManifest?.clientModules;
|
||||
let filesBySource;
|
||||
let webpackLayout = false;
|
||||
if (entries && typeof entries === "object" && !Array.isArray(entries)) {
|
||||
const sourceEntries = Object.keys(entries);
|
||||
if (
|
||||
!sourceEntries.some((key) =>
|
||||
key.replaceAll("\\", "/").endsWith(`/app${appPath}`),
|
||||
)
|
||||
)
|
||||
throw new Error("Route page entry is absent from entryJSFiles.");
|
||||
filesBySource = Object.entries(entries);
|
||||
} else if (webpackModules && typeof webpackModules === "object") {
|
||||
webpackLayout = true;
|
||||
// Each `chunks` array is `[chunkId, fileName, chunkId, fileName, ...]`.
|
||||
filesBySource = [];
|
||||
for (const node of Object.values(webpackModules)) {
|
||||
if (!Array.isArray(node?.chunks) || node.chunks.length === 0) continue;
|
||||
// One shared origin label instead of the module path: the per-chunk
|
||||
// `sources` list is written into report.json, and webpack records
|
||||
// absolute node_modules paths for every client module on the route.
|
||||
filesBySource.push(["client-module", node.chunks]);
|
||||
}
|
||||
if (filesBySource.length === 0)
|
||||
throw new Error(
|
||||
"Neither entryJSFiles nor clientModules chunk data is available; this manifest layout is not supported.",
|
||||
);
|
||||
} else {
|
||||
throw new Error(
|
||||
"entryJSFiles is unavailable; this manifest layout is not supported.",
|
||||
);
|
||||
const sourceEntries = Object.keys(entries);
|
||||
if (
|
||||
!sourceEntries.some((key) =>
|
||||
key.replaceAll("\\", "/").endsWith(`/app${appPath}`),
|
||||
)
|
||||
)
|
||||
throw new Error("Route page entry is absent from entryJSFiles.");
|
||||
}
|
||||
const bootstrap = filesFrom(
|
||||
buildManifest.rootMainFilesTree?.[appPath] ?? buildManifest.rootMainFiles,
|
||||
);
|
||||
@@ -110,8 +157,9 @@ export function measureRoute({
|
||||
origins.set(file, sources);
|
||||
};
|
||||
for (const file of bootstrap) add(file, "bootstrap");
|
||||
for (const [entry, values] of Object.entries(entries))
|
||||
for (const file of filesFrom(values)) add(file, entry);
|
||||
const readChunkList = webpackLayout ? assetFilesFromChunkList : filesFrom;
|
||||
for (const [entry, values] of filesBySource)
|
||||
for (const file of readChunkList(values)) add(file, entry);
|
||||
const size = (file, sources) => {
|
||||
const bytes = readAsset(file);
|
||||
return {
|
||||
@@ -248,12 +296,13 @@ export function collectReport(nextDir, config, metadata = {}) {
|
||||
"Optional PERFORMANCE_COMMIT_SHA supplied by the build caller; not inferred from current checkout.",
|
||||
nodeVersion: process.version,
|
||||
zlibVersion: process.versions.zlib,
|
||||
manifestFormat: "Next App Router client-reference entryJSFiles",
|
||||
manifestFormat:
|
||||
"Turbopack: client-reference entryJSFiles. Webpack: deduplicated clientModules[*].chunks.",
|
||||
definition:
|
||||
"Initial entry envelope: deduplicated rootMainFiles bootstrap plus all entryJSFiles in this route's client-reference manifest, including boundary/loading entries. This is emitted file size, not measured browser traffic or a load-time benchmark.",
|
||||
"Initial entry envelope: deduplicated rootMainFiles bootstrap plus every client chunk this route's client-reference manifest lists, including boundary/loading entries. Turbopack exposes these as entryJSFiles; webpack exposes them only through clientModules[*].chunks, so the same envelope is derived from whichever the build emitted. This is emitted file size, not measured browser traffic or a load-time benchmark.",
|
||||
gzip: "Sum of each unique JavaScript file independently compressed with Node gzip level 9. Excludes HTTP headers and shared-cache reuse.",
|
||||
excluded:
|
||||
"CSS, source maps, images, RSC/HTML payloads, external scripts, async-only chunks absent from entryJSFiles; legacy nomodule polyfills are reported separately.",
|
||||
"CSS, source maps, images, RSC/HTML payloads, external scripts, async-only chunks absent from the manifest's chunk lists; legacy nomodule polyfills are reported separately.",
|
||||
routes,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -216,6 +216,135 @@ describe("route JS measurement", () => {
|
||||
);
|
||||
expect(collectReport(dir, config).routes[0].status).toBe("unavailable");
|
||||
});
|
||||
// The regression: after the build moved to webpack (3d828a61) the manifest
|
||||
// has no entryJSFiles, only clientModules[*].chunks. Every route then
|
||||
// reported "unavailable" and the report measured nothing at all while still
|
||||
// exiting zero, so the budgets silently stopped being enforced.
|
||||
describe("webpack manifests without entryJSFiles", () => {
|
||||
const webpackManifest = {
|
||||
clientModules: {
|
||||
"[project]/src/components/header.tsx": {
|
||||
chunks: [
|
||||
"4269",
|
||||
"static/chunks/4269-shared.js?dpl=abc",
|
||||
"6726",
|
||||
"static/chunks/header-entry.js?dpl=abc",
|
||||
],
|
||||
},
|
||||
"[project]/src/app/(site)/news/page.tsx": {
|
||||
chunks: [
|
||||
"4269",
|
||||
"static/chunks/4269-shared.js?dpl=abc",
|
||||
"7777",
|
||||
"/_next/static/chunks/page.js?dpl=abc",
|
||||
],
|
||||
},
|
||||
// Async-only modules are recorded with an empty chunk list.
|
||||
"[project]/src/components/lazy.tsx": { chunks: [] },
|
||||
},
|
||||
};
|
||||
const webpackFiles = {
|
||||
// buildManifest.rootMainFiles lists runtime.js and shared.js, so both
|
||||
// bootstrap assets must exist or the read fails.
|
||||
"static/chunks/runtime.js": Buffer.from("const runtime = true;"),
|
||||
"static/chunks/shared.js": Buffer.from("bootstrap".repeat(10)),
|
||||
"static/chunks/4269-shared.js": Buffer.from("shared".repeat(50)),
|
||||
"static/chunks/header-entry.js": Buffer.from("header"),
|
||||
"static/chunks/page.js": Buffer.from("page"),
|
||||
"static/chunks/polyfill.js": Buffer.from("legacy"),
|
||||
};
|
||||
const measure = () =>
|
||||
measureRoute({
|
||||
budget,
|
||||
appPath,
|
||||
buildManifest,
|
||||
clientManifest: webpackManifest,
|
||||
readAsset: (file) => webpackFiles[file],
|
||||
});
|
||||
|
||||
it("derives the envelope from clientModules and ignores chunk ids", () => {
|
||||
const row = measure();
|
||||
expect(row.status).toBe("measured");
|
||||
// 2 bootstrap + shared + header-entry + page; the numeric chunk ids
|
||||
// are not assets and must not throw or be counted.
|
||||
expect(row.initial.chunkCount).toBe(5);
|
||||
expect(row.initial.chunks.map((f) => f.path).sort()).toEqual([
|
||||
"static/chunks/4269-shared.js",
|
||||
"static/chunks/header-entry.js",
|
||||
"static/chunks/page.js",
|
||||
"static/chunks/runtime.js",
|
||||
"static/chunks/shared.js",
|
||||
]);
|
||||
// Same bytes as the Turbopack fixture would produce for these files.
|
||||
expect(row.initial.rawBytes).toBe(
|
||||
Object.values(webpackFiles)
|
||||
.filter((b) => !b.includes("legacy"))
|
||||
.reduce((n, b) => n + b.length, 0),
|
||||
);
|
||||
});
|
||||
|
||||
it("counts a chunk reached by several client modules only once", () => {
|
||||
const shared = measure().initial.chunks.find(
|
||||
(f) => f.path === "static/chunks/4269-shared.js",
|
||||
);
|
||||
expect(shared).toBeDefined();
|
||||
expect(measure().initial.chunkCount).toBe(5);
|
||||
});
|
||||
|
||||
it("does not leak absolute module paths into the report", () => {
|
||||
const sources = new Set(
|
||||
measure().initial.chunks.flatMap((chunk) => chunk.sources),
|
||||
);
|
||||
// Only the two known origin labels; no node_modules path may appear.
|
||||
expect([...sources].sort()).toEqual(["bootstrap", "client-module"]);
|
||||
});
|
||||
|
||||
it("still fails rather than under-reporting a malformed chunk path", () => {
|
||||
expect(() =>
|
||||
measureRoute({
|
||||
budget,
|
||||
appPath,
|
||||
buildManifest,
|
||||
clientManifest: {
|
||||
clientModules: {
|
||||
x: { chunks: ["static/chunks/../../etc/passwd"] },
|
||||
},
|
||||
},
|
||||
readAsset: (file) => webpackFiles[file],
|
||||
}),
|
||||
).toThrow("Unsupported JavaScript asset");
|
||||
});
|
||||
|
||||
it("reports unavailable when webpack recorded no chunks at all", () => {
|
||||
expect(() =>
|
||||
measureRoute({
|
||||
budget,
|
||||
appPath,
|
||||
buildManifest,
|
||||
clientManifest: { clientModules: { x: { chunks: [] } } },
|
||||
readAsset: (file) => webpackFiles[file],
|
||||
}),
|
||||
).toThrow("Neither entryJSFiles nor clientModules");
|
||||
});
|
||||
|
||||
it("prefers entryJSFiles when a manifest carries both", () => {
|
||||
// A future Next version could emit both; the explicit list wins
|
||||
// because it is per-segment and therefore the tighter envelope.
|
||||
const row = measureRoute({
|
||||
budget,
|
||||
appPath,
|
||||
buildManifest,
|
||||
clientManifest: {
|
||||
...webpackManifest,
|
||||
entryJSFiles: {
|
||||
"[project]/src/app/(site)/news/page": ["static/chunks/page.js"],
|
||||
},
|
||||
},
|
||||
readAsset: (file) => webpackFiles[file],
|
||||
});
|
||||
expect(row.initial.chunkCount).toBe(3);
|
||||
});
|
||||
});
|
||||
it("does not deduplicate shared files across independent cold route totals", () => {
|
||||
const row = measureRoute({
|
||||
budget,
|
||||
|
||||
@@ -3,7 +3,6 @@ import {
|
||||
copyFileSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
rmSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
@@ -85,93 +84,3 @@ it.skipIf(!hasCompose)(
|
||||
},
|
||||
30_000,
|
||||
);
|
||||
|
||||
it("documents the local CrowdSec switches in .env.example", () => {
|
||||
const examples = readFileSync(path.join(root, ".env.example"), "utf8");
|
||||
for (const key of [
|
||||
"CROWDSEC_LOCAL_ENABLED",
|
||||
"CROWDSEC_LAPI_URL",
|
||||
"CROWDSEC_LAPI_PORT",
|
||||
"CROWDSEC_LAPI_API_KEY",
|
||||
"CROWDSEC_NGINX_LOG_DIR",
|
||||
]) {
|
||||
expect(examples).toContain(key);
|
||||
}
|
||||
});
|
||||
|
||||
it.skipIf(!hasCompose)(
|
||||
"renders the standalone CrowdSec stack with a loopback-only LAPI",
|
||||
() => {
|
||||
const directory = mkdtempSync(path.join(tmpdir(), "cms-crowdsec-"));
|
||||
try {
|
||||
mkdirSync(path.join(directory, "deployment/crowdsec/acquis.d"), {
|
||||
recursive: true,
|
||||
});
|
||||
copyFileSync(
|
||||
path.join(root, "deployment/crowdsec/compose.crowdsec.yml"),
|
||||
path.join(directory, "deployment/crowdsec/compose.crowdsec.yml"),
|
||||
);
|
||||
copyFileSync(
|
||||
path.join(root, "deployment/crowdsec/acquis.d/nginx.yaml"),
|
||||
path.join(directory, "deployment/crowdsec/acquis.d/nginx.yaml"),
|
||||
);
|
||||
writeFileSync(
|
||||
path.join(directory, ".env"),
|
||||
[
|
||||
"CROWDSEC_LAPI_API_KEY=fixture-key",
|
||||
"CROWDSEC_LAPI_PORT=18080",
|
||||
"CROWDSEC_LAPI_URL=http://127.0.0.1:18080",
|
||||
"CROWDSEC_NGINX_LOG_DIR=/var/log/nginx",
|
||||
].join("\n"),
|
||||
);
|
||||
const environment = { ...process.env };
|
||||
for (const key of Object.keys(environment))
|
||||
if (
|
||||
key.startsWith("COMPOSE_") ||
|
||||
key.startsWith("CROWDSEC_") ||
|
||||
key.startsWith("TZ")
|
||||
)
|
||||
delete environment[key];
|
||||
const result = spawnSync(
|
||||
"docker",
|
||||
[
|
||||
"compose",
|
||||
"--project-name",
|
||||
"crowdsec-fixture",
|
||||
"--env-file",
|
||||
".env",
|
||||
"-f",
|
||||
"deployment/crowdsec/compose.crowdsec.yml",
|
||||
"--profile",
|
||||
"security",
|
||||
"config",
|
||||
"--format",
|
||||
"json",
|
||||
],
|
||||
{ cwd: directory, env: environment, encoding: "utf8", timeout: 15_000 },
|
||||
);
|
||||
expect(result.status, result.stderr).toBe(0);
|
||||
const config = JSON.parse(result.stdout);
|
||||
const service = config.services.crowdsec;
|
||||
expect(service).toBeDefined();
|
||||
expect(service.image).toContain("crowdsecurity/crowdsec:");
|
||||
expect(service.environment.BOUNCER_KEY_cms).toBe("fixture-key");
|
||||
expect(service.environment.DISABLE_ONLINE_API).toBe("true");
|
||||
expect(
|
||||
service.ports.some(
|
||||
(published) =>
|
||||
published.host_ip === "127.0.0.1" &&
|
||||
published.published === "18080" &&
|
||||
published.target === 8080,
|
||||
),
|
||||
).toBe(true);
|
||||
const targets = service.volumes.map((volume) => volume.target);
|
||||
expect(targets).toContain("/var/log/nginx");
|
||||
expect(targets).toContain("/etc/crowdsec/acquis.d");
|
||||
expect(service.healthcheck.test.join(" ")).toContain("wget");
|
||||
} finally {
|
||||
rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
},
|
||||
30_000,
|
||||
);
|
||||
+28
-3
@@ -1,10 +1,35 @@
|
||||
#!/usr/bin/env bash
|
||||
# Setup cron jobs for maintenance
|
||||
#
|
||||
# Appends to the existing crontab. `crontab -` replaces the whole file, so a
|
||||
# script that pipes one job at a time silently drops every other scheduled job.
|
||||
# Entries are matched by their command, so re-running this is idempotent.
|
||||
set -Eeuo pipefail
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# Adds one schedule line to the crontab unless its command is already present.
|
||||
add_job() {
|
||||
local schedule="$1" command
|
||||
command="${schedule##* }"
|
||||
local current
|
||||
current="$(crontab -l 2>/dev/null || true)"
|
||||
if grep -Fq "$command" <<<"$current"; then
|
||||
echo "Already scheduled: $command"
|
||||
return
|
||||
fi
|
||||
if [[ -z "$current" ]]; then
|
||||
printf '%s\n' "$schedule" | crontab -
|
||||
else
|
||||
printf '%s\n%s\n' "$current" "$schedule" | crontab -
|
||||
fi
|
||||
echo "Scheduled: $schedule"
|
||||
}
|
||||
|
||||
# Run backup every day at 03:00
|
||||
echo "0 3 * * * $SCRIPT_DIR/backup.sh" | crontab -
|
||||
# Run prune every Sunday at 04:00
|
||||
echo "0 4 * * 0 $SCRIPT_DIR/docker-prune.sh" | crontab -
|
||||
add_job "0 3 * * * $SCRIPT_DIR/backup.sh"
|
||||
# Run prune every day at 04:00. Daily rather than weekly: byparr leaks roughly
|
||||
# 1.7 GB/day of orphaned browser profiles into its writable layer, so a weekly
|
||||
# run would let ~12 GB accumulate before anything reclaimed it.
|
||||
add_job "0 4 * * * $SCRIPT_DIR/docker-prune.sh"
|
||||
|
||||
echo "Cron jobs configured."
|
||||
@@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env node
|
||||
import { readFileSync, writeFileSync } from "node:fs";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
|
||||
const projectRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
|
||||
/**
|
||||
* Collapse the version facts the README advertises into a single object, read
|
||||
* from the source of truth (`package.json` + `.nvmrc`). Kept as a pure function
|
||||
* so the test suite can pin the extraction without touching the filesystem.
|
||||
*/
|
||||
export function collectVersions(pkg, nvmrc) {
|
||||
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
||||
const require = (name) => {
|
||||
const value = deps[name];
|
||||
if (typeof value !== "string" || !/^\d+\.\d+/.test(value))
|
||||
throw new Error(`package.json is missing a concrete ${name} version`);
|
||||
return value;
|
||||
};
|
||||
|
||||
const pnpm = /^pnpm@([^+]+)/.exec(pkg.packageManager ?? "")?.[1];
|
||||
if (!pnpm) throw new Error("package.json packageManager must pin pnpm");
|
||||
|
||||
const node = nvmrc.trim();
|
||||
if (!/^\d+\.\d+\.\d+$/.test(node))
|
||||
throw new Error(".nvmrc must pin an exact Node.js version");
|
||||
|
||||
return {
|
||||
pnpm,
|
||||
node,
|
||||
next: require("next"),
|
||||
react: require("react"),
|
||||
typescript: require("typescript"),
|
||||
drizzleOrm: require("drizzle-orm"),
|
||||
zod: require("zod"),
|
||||
vitest: require("vitest"),
|
||||
biome: require("@biomejs/biome"),
|
||||
playwright: require("@playwright/test"),
|
||||
};
|
||||
}
|
||||
|
||||
/** The single "Core stack" line, built from the live versions. */
|
||||
export function coreStackLine(v) {
|
||||
return `Core stack: **Next.js ${v.next}** (App Router) · **React ${v.react}** · **TypeScript ${v.typescript}** · **Drizzle ORM ${v.drizzleOrm}** · **Zod ${v.zod}** · **Vitest ${v.vitest}** · **Biome ${v.biome}** · **Playwright ${v.playwright}**.`;
|
||||
}
|
||||
|
||||
const CORE_STACK_RE = /^Core stack: \*\*Next\.js .*$/m;
|
||||
|
||||
/**
|
||||
* Rewrite every managed version reference in `readme` to match `versions`.
|
||||
* Returns the new text plus a human-readable list of what changed. Throws if a
|
||||
* managed anchor is missing, so a README restructure fails loudly instead of
|
||||
* silently dropping out of sync.
|
||||
*/
|
||||
export function syncReadmeVersions(readme, versions) {
|
||||
const changes = [];
|
||||
let next = readme;
|
||||
|
||||
// `| Node.js | 26.10.0 | … (>=26.10.0 <27) |` — version cell + engines note.
|
||||
next = replaceVersionCell(
|
||||
next,
|
||||
"Node\\.js",
|
||||
versions.node,
|
||||
"Node.js",
|
||||
changes,
|
||||
);
|
||||
const nodeMajor = Number.parseInt(versions.node.split(".")[0], 10);
|
||||
const nodeNote = /(>=)\d+\.\d+\.\d+( <)\d+/;
|
||||
if (!nodeNote.test(next))
|
||||
throw new Error("README is missing the Node.js engines note");
|
||||
next = next.replace(nodeNote, `$1${versions.node}$2${nodeMajor + 1}`);
|
||||
|
||||
// `| pnpm | 12.11.2 | … |`.
|
||||
next = replaceVersionCell(next, "pnpm", versions.pnpm, "pnpm", changes);
|
||||
|
||||
// `Core stack: **Next.js …** · …`.
|
||||
const stackMatch = CORE_STACK_RE.exec(next);
|
||||
if (!stackMatch) throw new Error("README is missing the core stack line");
|
||||
const expectedStack = coreStackLine(versions);
|
||||
if (stackMatch[0] !== expectedStack) {
|
||||
changes.push("core stack line");
|
||||
next = next.replace(CORE_STACK_RE, expectedStack);
|
||||
}
|
||||
|
||||
return { readme: next, changes };
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the version token of a `| name | version | … |` table row, padding
|
||||
* the trailing spaces so the closing pipe stays in the same column when the
|
||||
* new version is a different length.
|
||||
*/
|
||||
function replaceVersionCell(readme, namePattern, version, label, changes) {
|
||||
const re = new RegExp(
|
||||
`^(\\| ${namePattern}(?: +)\\| )(\\S+)( +)(\\|.*)$`,
|
||||
"m",
|
||||
);
|
||||
const match = re.exec(readme);
|
||||
if (!match)
|
||||
throw new Error(`README is missing the ${label} requirements row`);
|
||||
const [, pre, current, pad, post] = match;
|
||||
if (current === version) return readme;
|
||||
changes.push(`${label} ${current} -> ${version}`);
|
||||
const padLength = Math.max(1, pad.length - (version.length - current.length));
|
||||
return readme.replace(re, `${pre}${version}${" ".repeat(padLength)}${post}`);
|
||||
}
|
||||
|
||||
function main() {
|
||||
const check = process.argv.includes("--check");
|
||||
const pkg = JSON.parse(
|
||||
readFileSync(resolve(projectRoot, "package.json"), "utf8"),
|
||||
);
|
||||
const nvmrc = readFileSync(resolve(projectRoot, ".nvmrc"), "utf8");
|
||||
const readmePath = resolve(projectRoot, "README.md");
|
||||
const readme = readFileSync(readmePath, "utf8");
|
||||
|
||||
const { readme: updated, changes } = syncReadmeVersions(
|
||||
readme,
|
||||
collectVersions(pkg, nvmrc),
|
||||
);
|
||||
|
||||
if (changes.length === 0) {
|
||||
console.log("README versions are in sync with package.json / .nvmrc");
|
||||
return;
|
||||
}
|
||||
|
||||
if (check) {
|
||||
console.error("README versions are out of date:");
|
||||
for (const change of changes) console.error(` - ${change}`);
|
||||
console.error("Run `pnpm readme:versions` to fix.");
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
writeFileSync(readmePath, updated);
|
||||
console.log("README versions updated:");
|
||||
for (const change of changes) console.log(` - ${change}`);
|
||||
}
|
||||
|
||||
if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) main();
|
||||
@@ -0,0 +1,113 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
collectVersions,
|
||||
coreStackLine,
|
||||
syncReadmeVersions,
|
||||
} from "./sync-readme-versions.mjs";
|
||||
|
||||
const versions = {
|
||||
pnpm: "12.11.2",
|
||||
node: "26.10.0",
|
||||
next: "16.4.1",
|
||||
react: "19.3.1",
|
||||
typescript: "7.0.2",
|
||||
drizzleOrm: "0.45.4",
|
||||
zod: "4.6.7",
|
||||
vitest: "5.0.4",
|
||||
biome: "2.5.15",
|
||||
playwright: "1.64.1",
|
||||
};
|
||||
|
||||
const pkg = {
|
||||
packageManager: `pnpm@${versions.pnpm}+sha512.deadbeef`,
|
||||
dependencies: {
|
||||
next: versions.next,
|
||||
react: versions.react,
|
||||
zod: versions.zod,
|
||||
},
|
||||
devDependencies: {
|
||||
typescript: versions.typescript,
|
||||
"drizzle-orm": versions.drizzleOrm,
|
||||
vitest: versions.vitest,
|
||||
"@biomejs/biome": versions.biome,
|
||||
"@playwright/test": versions.playwright,
|
||||
},
|
||||
};
|
||||
|
||||
const readme = [
|
||||
"| Node.js | 26.10.0 | Pinned in `.nvmrc` (`>=26.10.0 <27`) |",
|
||||
"| pnpm | 12.11.2 | Pinned via `packageManager` |",
|
||||
"",
|
||||
coreStackLine(versions),
|
||||
"",
|
||||
].join("\n");
|
||||
|
||||
describe("collectVersions", () => {
|
||||
it("pulls every managed version from package.json and .nvmrc", () => {
|
||||
expect(collectVersions(pkg, `${versions.node}\n`)).toEqual(versions);
|
||||
});
|
||||
|
||||
it("rejects a packageManager without a pinned pnpm", () => {
|
||||
expect(() =>
|
||||
collectVersions({ ...pkg, packageManager: "npm@10" }, "26.10.0"),
|
||||
).toThrow(/pnpm/);
|
||||
});
|
||||
|
||||
it("rejects a ranged dependency instead of a pinned one", () => {
|
||||
const ranged = {
|
||||
...pkg,
|
||||
dependencies: { ...pkg.dependencies, next: "^16" },
|
||||
};
|
||||
expect(() => collectVersions(ranged, "26.10.0")).toThrow(/next/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("syncReadmeVersions", () => {
|
||||
it("reports no changes when the README already matches", () => {
|
||||
const result = syncReadmeVersions(readme, versions);
|
||||
expect(result.changes).toEqual([]);
|
||||
expect(result.readme).toBe(readme);
|
||||
});
|
||||
|
||||
it("updates the pnpm row and keeps the table aligned", () => {
|
||||
const bumped = { ...versions, pnpm: "13.0.0" };
|
||||
const original = readme.split("\n").find((line) => line.includes("pnpm"));
|
||||
const { readme: updated, changes } = syncReadmeVersions(readme, bumped);
|
||||
const line = updated.split("\n").find((l) => l.includes("pnpm"));
|
||||
|
||||
expect(changes).toContain("pnpm 12.11.2 -> 13.0.0");
|
||||
expect(line).toContain("| 13.0.0");
|
||||
expect(line?.length).toBe(original?.length);
|
||||
expect(line?.endsWith("|\n") || line?.endsWith("|")).toBe(true);
|
||||
});
|
||||
|
||||
it("rewrites the core stack line from the live versions", () => {
|
||||
const bumped = { ...versions, next: "17.0.0", zod: "5.0.0" };
|
||||
const { readme: updated, changes } = syncReadmeVersions(readme, bumped);
|
||||
expect(changes).toContain("core stack line");
|
||||
expect(updated).toContain(coreStackLine(bumped));
|
||||
expect(updated).not.toContain("Next.js 16.4.1");
|
||||
});
|
||||
|
||||
it("updates both the Node version cell and the engines note", () => {
|
||||
const bumped = { ...versions, node: "27.3.1" };
|
||||
const { readme: updated, changes } = syncReadmeVersions(readme, bumped);
|
||||
expect(changes).toContain("Node.js 26.10.0 -> 27.3.1");
|
||||
expect(updated).toContain("| 27.3.1 ");
|
||||
expect(updated).toContain("`>=27.3.1 <28`");
|
||||
});
|
||||
|
||||
it("fails loudly when a managed anchor is missing", () => {
|
||||
expect(() =>
|
||||
syncReadmeVersions("no table here\nno core stack", versions),
|
||||
).toThrow(/requirements row/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("coreStackLine", () => {
|
||||
it("renders the documented ordering", () => {
|
||||
expect(coreStackLine(versions)).toBe(
|
||||
"Core stack: **Next.js 16.4.1** (App Router) · **React 19.3.1** · **TypeScript 7.0.2** · **Drizzle ORM 0.45.4** · **Zod 4.6.7** · **Vitest 5.0.4** · **Biome 2.5.15** · **Playwright 1.64.1**.",
|
||||
);
|
||||
});
|
||||
});
|
||||
Executable
+228
@@ -0,0 +1,228 @@
|
||||
#!/usr/bin/env bash
|
||||
# Voer een zwaar commando uit onder een harde geheugenplafond.
|
||||
#
|
||||
# Waarom dit bestaat:
|
||||
# De host draait met `vm.overcommit_memory=0` en ZONDER swap. Vraagt een
|
||||
# proces meer geheugen dan er vrij is, dan geeft de kernel niets weg en
|
||||
# roept hij meteen de OOM-killer aan. Die kiest zijn slachtoffer over de
|
||||
# héle machine, niet alleen in het schuldige proces — dus een build kan de
|
||||
# database, nginx of de live release meenemen.
|
||||
#
|
||||
# `next build` op Turbopack groeit op dit 329-route app voorbij 12GB RSS
|
||||
# en is ook met 4GB swap nog steeds dood. Zie commit 3d828a61.
|
||||
#
|
||||
# Wat dit doet:
|
||||
# Het commando komt in zijn eigen cgroup met `MemoryMax`. Als het door die
|
||||
# grens heen groeit krijgt alleen die cgroup een OOM-signaal: het commando
|
||||
# zelf sterft, de rest van de machine leeft. Dat is precies het gedrag dat
|
||||
# je wilt — de build faalt, de site blijft staan.
|
||||
#
|
||||
# Met `--max-old-space-size` lukt dat niet. Die limiet zit op de V8-heap en
|
||||
# Turbopack-geheugen is native Rust-geheugen; met een 2GB cap piekte de RSS
|
||||
# alsnog op 8GB. Zie het commentaar in de Dockerfile.
|
||||
#
|
||||
# Backends:
|
||||
#
|
||||
# systemd (cgroup MemoryMax)
|
||||
# Meet RSS over de héle procesboom. Dit is de echte garantie en wordt
|
||||
# overal gebruikt waar systemd beschikbaar is (de host waar deze
|
||||
# CMS draait). De plafonds in package.json zijn hierop gekozen:
|
||||
# `next build` piekt op ~9,7GB in `memory.current` (inclusief de
|
||||
# page cache die de build zelf opbouwt), dus 10GB laat net genoeg ruimte
|
||||
# over terwijl er ~6-9GB basislast naast blijft passen op deze machine.
|
||||
#
|
||||
# Twee extra vangnetten, omdat de basislast inmiddels groter kan zijn
|
||||
# (Docker, de emulator en de agent tikken samen ~9GB aan) en een vaste cap
|
||||
# die hoger is dan het vrije geheugen de host alsnog kan laten OOM-killen:
|
||||
# - MemoryHigh (95% van de cap) dwingt de kernel eerder te reclaimen en
|
||||
# te throttlen, zodat een allocatiestorm de harde grens niet voorbij
|
||||
# schiet voordat de cgroup-OOM-killer reageert;
|
||||
# - het plafond wordt teruggeschroefd tot `MemAvailable - reserve`, met
|
||||
# `CMS_MEMORY_CAP_RESERVE` (standaard 4g) als marge voor de rest van
|
||||
# de machine. Is er minder dan 1GB over, dan weigert het script.
|
||||
# MemorySwapMax=0 staat expliciet aan: deze host heeft geen swap.
|
||||
#
|
||||
# ulimit -v (per proces, virtuele adresruimte)
|
||||
# Alleen als expliciet gevraagd. Meet virtuele adresruimte, NIET RSS, en
|
||||
# kan de boom helemaal niet begrenzen: elke worker krijgt z'n eigen
|
||||
# limiet. Op moderne V8 is het bovendien een vergiftigde gift: `-v 10g`
|
||||
# laat V8 de heaplimiet terugbrengen naar 2,25GB (webpack sterft met
|
||||
# std::bad_alloc), en `-v 20g` laat de v8-wasm-memory-toewijzing falen
|
||||
# tijdens `next build`. Zet CMS_MEMORY_CAP_VIRTUAL ruim boven de fysieke
|
||||
# RAM als je het echt wilt gebruiken.
|
||||
#
|
||||
# Geen van beide -> weigeren. Stil onbegrensd doorlopen zou precies de
|
||||
# valse geruststelling zijn waar 3d828a61 voor waarschuwt. Omgevingen
|
||||
# zonder systemd (de Docker-build, de GitLab-runner) kiezen daarom
|
||||
# expliciet voor CMS_MEMORY_CAP_BACKEND=none — met een waarschuwing,
|
||||
# en met als rechtvaardiging dat die builds al begrensd zijn door
|
||||
# `next build --webpack` + `--max-old-space-size` en in hun eigen
|
||||
# geïsoleerde container draaien, niet op de host.
|
||||
#
|
||||
# Markering:
|
||||
# Elke backend zet `CMS_MEMORY_CAPPED=1` voordat het commando start.
|
||||
# `next.config.ts` weigert een productie-build zonder die markering, zodat
|
||||
# een handmatig `npx next build` (of een IDE/agent die de build zelf
|
||||
# start) niet meer onbeperkt geheugen kan vragen en de hele host mee
|
||||
# neemt. `CMS_MEMORY_CAP_BACKEND=none` telt mee: die omgevingen draaien
|
||||
# al in een eigen, geïsoleerde container.
|
||||
#
|
||||
# Gebruik: bash scripts/with-memory-cap.sh 10g <command...>
|
||||
# Backend kiezen: CMS_MEMORY_CAP_BACKEND=systemd|ulimit|none|auto
|
||||
# ulimit-waarde kiezen: CMS_MEMORY_CAP_VIRTUAL=40g
|
||||
# Marge bovenop de basislast: CMS_MEMORY_CAP_RESERVE=4g
|
||||
set -Eeuo pipefail
|
||||
|
||||
usage() {
|
||||
echo "gebruik: $0 <plafond, bv. 10g> <command...>" >&2
|
||||
exit 64
|
||||
}
|
||||
|
||||
[ "$#" -ge 2 ] || usage
|
||||
cap="$1"
|
||||
shift
|
||||
|
||||
# Zet 10g / 512m / 2G / 1234567 om in bytes. Alleen bytes gaan naar
|
||||
# systemd: MemoryMax accepteert `10G` maar weigert `10g`, en die
|
||||
# hoofdletterval is te makkelijk om per ongeluk te treffen.
|
||||
to_bytes() {
|
||||
local value="$1" number suffix
|
||||
if [[ "$value" =~ ^([0-9]+)([kKmMgGtT]?)$ ]]; then
|
||||
number="${BASH_REMATCH[1]}"
|
||||
suffix="${BASH_REMATCH[2]}"
|
||||
else
|
||||
echo "onbekend plafond-formaat: $value" >&2
|
||||
return 1
|
||||
fi
|
||||
case "$suffix" in
|
||||
k | K) echo $((number * 1024)) ;;
|
||||
m | M) echo $((number * 1024 * 1024)) ;;
|
||||
g | G) echo $((number * 1024 * 1024 * 1024)) ;;
|
||||
t | T) echo $((number * 1024 * 1024 * 1024 * 1024)) ;;
|
||||
*) echo "$number" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
bytes="$(to_bytes "$cap")" || exit 64
|
||||
kilobytes=$((bytes / 1024))
|
||||
|
||||
# De virtuele waarde voor ulimit -v. Bewust los van `cap`: zie de toelichting
|
||||
# hierboven, 10g -v breekt de webpack-build.
|
||||
virtual_bytes="$(to_bytes "${CMS_MEMORY_CAP_VIRTUAL:-40g}")" || exit 64
|
||||
virtual_kb=$((virtual_bytes / 1024))
|
||||
|
||||
backend="${CMS_MEMORY_CAP_BACKEND:-auto}"
|
||||
systemd_cmd=()
|
||||
|
||||
# Vanaf hier is dit script de enige plek waar een zwaar commando nog mag
|
||||
# starten. De markering maakt dat afdwingbaar in next.config.ts.
|
||||
export CMS_MEMORY_CAPPED=1
|
||||
|
||||
# Echt proberen, niet alleen uitzoeken of het bestand bestaat: `systemd-run`
|
||||
# zonder rechten faalt met "Access denied", en dat moet dan een nette
|
||||
# terugval naar ulimit worden in plaats van een kapotte build.
|
||||
probe_systemd() {
|
||||
command -v systemd-run >/dev/null 2>&1 || return 1
|
||||
[ -d /run/systemd/system ] || return 1
|
||||
if systemd-run --scope --quiet true 2>/dev/null; then
|
||||
systemd_cmd=(systemd-run --scope --quiet)
|
||||
return 0
|
||||
fi
|
||||
if systemd-run --user --scope --quiet true 2>/dev/null; then
|
||||
systemd_cmd=(systemd-run --user --scope --quiet)
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
# Beschikbaar geheugen in bytes: wat het systeem kan toewijzen zonder te
|
||||
# swappen. Dit is inclusief opvraagbare page cache, dus de juiste maat om te
|
||||
# bepalen hoeveel een nieuwe cgroup nog mag gebruiken zonder de host in de
|
||||
# globale OOM-killer te duwen.
|
||||
mem_available_bytes() {
|
||||
local kb
|
||||
kb="$(grep -m1 '^MemAvailable:' /proc/meminfo 2>/dev/null | tr -dc '0-9')"
|
||||
[ -n "$kb" ] || kb=0
|
||||
echo $((kb * 1024))
|
||||
}
|
||||
|
||||
run_systemd() {
|
||||
local effective="$bytes"
|
||||
# Houd marge voor de al draaiende basislast (MariaDB, Docker, de live
|
||||
# release, de agent) plus kernel/page cache. Zonder die marge kan een cap
|
||||
# die hoger is dan het werkelijk beschikbare geheugen alsnog de host laten
|
||||
# OOM-killen voordat de cgroup-grens grijpt.
|
||||
local reserve
|
||||
reserve="$(to_bytes "${CMS_MEMORY_CAP_RESERVE:-4g}")" || exit 64
|
||||
local available
|
||||
available="$(mem_available_bytes)"
|
||||
if [ "$available" -gt 0 ]; then
|
||||
local headroom=$((available > reserve ? available - reserve : 0))
|
||||
if [ "$headroom" -lt "$effective" ]; then
|
||||
echo "[mem-cap] plafond verlaagd naar $((headroom / 1024 / 1024))MB: MemAvailable=$((available / 1024 / 1024))MB reserve=$((reserve / 1024 / 1024))MB" >&2
|
||||
effective="$headroom"
|
||||
fi
|
||||
fi
|
||||
if [ "$effective" -lt $((1024 * 1024 * 1024)) ]; then
|
||||
echo "[mem-cap] te weinig vrij geheugen (MemAvailable=$((available / 1024 / 1024))MB) om begrensd te starten; weiger." >&2
|
||||
echo "[mem-cap] maak geheugen vrij of verhoog CMS_MEMORY_CAP_RESERVE bewust niet." >&2
|
||||
return 1
|
||||
fi
|
||||
# MemoryHigh laat de kernel proactief reclaimen en throttlen vóór de harde
|
||||
# MemoryMax, zodat een snelle allocatiestorm niet voorbij de grens schiet en
|
||||
# de (globale) OOM-killer de hele machine meeneemt. MemorySwapMax=0 is
|
||||
# expliciet: deze host heeft geen swap.
|
||||
local high=$((effective * 95 / 100))
|
||||
echo "[mem-cap] systemd cgroup MemoryHigh=$((high / 1024 / 1024))MB MemoryMax=$((effective / 1024 / 1024))MB MemorySwapMax=0: $*" >&2
|
||||
exec "${systemd_cmd[@]}" -p "MemoryHigh=$high" -p "MemoryMax=$effective" -p "MemorySwapMax=0" "$@"
|
||||
}
|
||||
|
||||
run_ulimit() {
|
||||
echo "[mem-cap] ulimit -v ${virtual_kb}KB per proces (geen systemd; RSS-plafond ${cap} niet meetbaar zonder cgroup): $*" >&2
|
||||
if [ "$virtual_bytes" -lt $((16 * 1024 * 1024 * 1024)) ]; then
|
||||
echo "[mem-cap] let op: -v onder 16g verlaagt V8's heaplimiet en breekt de build; verhoog CMS_MEMORY_CAP_VIRTUAL" >&2
|
||||
fi
|
||||
ulimit -v "$virtual_kb" || {
|
||||
echo "[mem-cap] ulimit -v $virtual_kb werd geweigerd" >&2
|
||||
return 1
|
||||
}
|
||||
exec "$@"
|
||||
}
|
||||
|
||||
run_refuse() {
|
||||
echo "[mem-cap] geen systemd hier; weiger onbegrensd te draaien." >&2
|
||||
echo "[mem-cap] zet CMS_MEMORY_CAP_BACKEND=none om dit bewust te accepteren, of =ulimit voor een per-proces vangnet." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
run_opted_out() {
|
||||
echo "[mem-cap] WAARSCHUWING: plafond bewust uitgeschakeld, dit commando kan de machine laten OOM-killed worden: $*" >&2
|
||||
exec "$@"
|
||||
}
|
||||
|
||||
case "$backend" in
|
||||
systemd)
|
||||
probe_systemd || {
|
||||
echo "[mem-cap] CMS_MEMORY_CAP_BACKEND=systemd maar systemd-run reageert niet" >&2
|
||||
exit 70
|
||||
}
|
||||
run_systemd "$@"
|
||||
;;
|
||||
ulimit)
|
||||
run_ulimit "$@"
|
||||
;;
|
||||
none | off)
|
||||
run_opted_out "$@"
|
||||
;;
|
||||
auto)
|
||||
if probe_systemd; then
|
||||
run_systemd "$@"
|
||||
else
|
||||
run_refuse "$@"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
echo "[mem-cap] onbekende backend: $backend" >&2
|
||||
exit 64
|
||||
;;
|
||||
esac
|
||||
@@ -34,7 +34,7 @@ export async function sendHotelAlert(formData: FormData): Promise<void> {
|
||||
|
||||
/** Mark every unread ops alert as read. */
|
||||
export async function markAllAlertsRead(): Promise<void> {
|
||||
await requirePermission(PERMS.NOTIFICATIONS_VIEW);
|
||||
await requirePermission(PERMS.NOTIFICATIONS_EDIT);
|
||||
try {
|
||||
await db
|
||||
.update(AlertLogs)
|
||||
|
||||
@@ -15,14 +15,6 @@ import {
|
||||
setLastCloudflareVerify,
|
||||
verifyCloudflareConnection,
|
||||
} from "@/lib/cloudflare-api";
|
||||
import {
|
||||
setLastCrowdsecVerify,
|
||||
verifyCrowdsecConnection,
|
||||
} from "@/lib/crowdsec-api";
|
||||
import {
|
||||
setLastCrowdsecReport,
|
||||
verifyCrowdsecReporting,
|
||||
} from "@/lib/crowdsec-report";
|
||||
import { db, WebsiteSetting } from "@/lib/db";
|
||||
import { logger } from "@/lib/logger";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
@@ -41,17 +33,6 @@ function positiveInt(raw: FormDataEntryValue | null, fallback: number): number {
|
||||
return Math.floor(n);
|
||||
}
|
||||
|
||||
function clampInt(
|
||||
raw: FormDataEntryValue | null,
|
||||
fallback: number,
|
||||
min: number,
|
||||
max: number,
|
||||
): number {
|
||||
const n = Number(str(raw));
|
||||
if (!Number.isFinite(n)) return fallback;
|
||||
return Math.min(max, Math.max(min, Math.floor(n)));
|
||||
}
|
||||
|
||||
function parseTiers(raw: FormDataEntryValue | null): AntiddosBlockTier[] {
|
||||
const tiers: AntiddosBlockTier[] = [];
|
||||
for (const part of str(raw).split(",")) {
|
||||
@@ -118,17 +99,6 @@ function configFromForm(formData: FormData): AntiddosConfig {
|
||||
defaults.globalHaltMs,
|
||||
),
|
||||
cloudflareAutoBlock: str(formData.get("cfa_auto_block")) === "1",
|
||||
crowdsecAutoBlock: str(formData.get("cs_auto_block")) === "1",
|
||||
crowdsecBlockScore: clampInt(
|
||||
formData.get("cs_block_score"),
|
||||
defaults.crowdsecBlockScore,
|
||||
0,
|
||||
5,
|
||||
),
|
||||
crowdsecBlockTtlSeconds: positiveInt(
|
||||
formData.get("cs_block_ttl_sec"),
|
||||
defaults.crowdsecBlockTtlSeconds,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -153,9 +123,6 @@ async function persistSettings(config: AntiddosConfig): Promise<void> {
|
||||
],
|
||||
["antiddos_global_halt_ms", String(config.globalHaltMs)],
|
||||
["antiddos_cfa_auto_block", config.cloudflareAutoBlock ? "1" : "0"],
|
||||
["antiddos_cs_auto_block", config.crowdsecAutoBlock ? "1" : "0"],
|
||||
["antiddos_cs_block_score", String(config.crowdsecBlockScore)],
|
||||
["antiddos_cs_block_ttl", String(config.crowdsecBlockTtlSeconds)],
|
||||
];
|
||||
await Promise.all(
|
||||
entries.map(([key, value]) =>
|
||||
@@ -173,7 +140,7 @@ async function persistSettings(config: AntiddosConfig): Promise<void> {
|
||||
* override the proxy reads, so the change is live within the proxy cache TTL.
|
||||
*/
|
||||
export async function saveAntiddosSettings(formData: FormData): Promise<void> {
|
||||
const staff = await requirePermission(PERMS.SETTINGS_VIEW);
|
||||
const staff = await requirePermission(PERMS.SETTINGS_EDIT);
|
||||
const config = configFromForm(formData);
|
||||
|
||||
try {
|
||||
@@ -198,7 +165,7 @@ export async function saveAntiddosSettings(formData: FormData): Promise<void> {
|
||||
* DB-persisted settings. The gate immediately falls back to env ANTI_DDOS_*.
|
||||
*/
|
||||
export async function resetAntiddosSettings(): Promise<void> {
|
||||
const staff = await requirePermission(PERMS.SETTINGS_VIEW);
|
||||
const staff = await requirePermission(PERMS.SETTINGS_EDIT);
|
||||
|
||||
try {
|
||||
if (redis) await redis.del(OVERRIDE_KEY);
|
||||
@@ -219,7 +186,7 @@ export async function resetAntiddosSettings(): Promise<void> {
|
||||
|
||||
/** Remove a single IP from the temporary DDoS block list. */
|
||||
export async function unbanAntiddosIp(formData: FormData): Promise<void> {
|
||||
const staff = await requirePermission(PERMS.SETTINGS_VIEW);
|
||||
const staff = await requirePermission(PERMS.SETTINGS_EDIT);
|
||||
const ip = str(formData.get("ip")).trim();
|
||||
if (!ip) return;
|
||||
|
||||
@@ -228,7 +195,6 @@ export async function unbanAntiddosIp(formData: FormData): Promise<void> {
|
||||
await Promise.all([
|
||||
redis.del(`antiddos:block:${ip}`),
|
||||
redis.del(`antiddos:block:meta:${ip}`),
|
||||
redis.del(`crowdsec:report:${ip}`),
|
||||
redis.del(`antiddos:v:${ip}`),
|
||||
]);
|
||||
}
|
||||
@@ -247,7 +213,7 @@ export async function unbanAntiddosIp(formData: FormData): Promise<void> {
|
||||
|
||||
/** Remove an automatic Cloudflare edge block for a tracked IP. */
|
||||
export async function removeCloudflareRule(formData: FormData): Promise<void> {
|
||||
const staff = await requirePermission(PERMS.SETTINGS_VIEW);
|
||||
const staff = await requirePermission(PERMS.SETTINGS_EDIT);
|
||||
const ip = str(formData.get("ip")).trim();
|
||||
if (!ip) return;
|
||||
|
||||
@@ -265,35 +231,9 @@ export async function removeCloudflareRule(formData: FormData): Promise<void> {
|
||||
revalidatePath("/admin/devops/antiddos");
|
||||
}
|
||||
|
||||
/** Test the configured CrowdSec API credentials against the CTI endpoint. */
|
||||
export async function verifyCrowdsecConfiguration(): Promise<void> {
|
||||
const staff = await requirePermission(PERMS.SETTINGS_VIEW);
|
||||
const status = await verifyCrowdsecConnection();
|
||||
await setLastCrowdsecVerify(status);
|
||||
logger.info("CrowdSec API configuration verified", {
|
||||
staff: staff.username,
|
||||
ok: status.ok,
|
||||
message: status.message,
|
||||
});
|
||||
revalidatePath("/admin/devops/antiddos");
|
||||
}
|
||||
|
||||
/** Test the CrowdSec signal-push (CAPI watcher) channel. */
|
||||
export async function verifyCrowdsecReportingConfiguration(): Promise<void> {
|
||||
const staff = await requirePermission(PERMS.SETTINGS_VIEW);
|
||||
const status = await verifyCrowdsecReporting();
|
||||
await setLastCrowdsecReport(status);
|
||||
logger.info("CrowdSec reporting configuration verified", {
|
||||
staff: staff.username,
|
||||
ok: status.ok,
|
||||
message: status.message,
|
||||
});
|
||||
revalidatePath("/admin/devops/antiddos");
|
||||
}
|
||||
|
||||
/** Test the configured Cloudflare API credentials against the zone. */
|
||||
export async function verifyCloudflareConfiguration(): Promise<void> {
|
||||
const staff = await requirePermission(PERMS.SETTINGS_VIEW);
|
||||
const staff = await requirePermission(PERMS.SETTINGS_EDIT);
|
||||
const status = await verifyCloudflareConnection();
|
||||
await setLastCloudflareVerify(status);
|
||||
logger.info("Cloudflare API configuration verified", {
|
||||
|
||||
@@ -8,12 +8,16 @@ const state = vi.hoisted(() => ({
|
||||
fail: false,
|
||||
giveBadge: vi.fn(async () => undefined),
|
||||
requirePermission: vi.fn(),
|
||||
tryGuardRank: vi.fn(),
|
||||
revalidatePath: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/admin/guard", () => ({
|
||||
requirePermission: state.requirePermission,
|
||||
}));
|
||||
vi.mock("@/lib/admin/rank-guard", () => ({
|
||||
tryGuardRank: state.tryGuardRank,
|
||||
}));
|
||||
vi.mock("@/lib/permissions", () => ({
|
||||
PERMS: { CATALOG_EDIT: "admin.catalog.edit" },
|
||||
}));
|
||||
@@ -55,6 +59,10 @@ beforeEach(() => {
|
||||
state.insertValues.mockResolvedValue([{}]);
|
||||
state.giveBadge.mockResolvedValue(undefined);
|
||||
state.requirePermission.mockResolvedValue({ id: 1, rank: 7, username: "a" });
|
||||
state.tryGuardRank.mockResolvedValue({
|
||||
ok: true,
|
||||
target: { username: "u", rank: 1, mail: null },
|
||||
});
|
||||
});
|
||||
|
||||
describe("giveBadge", () => {
|
||||
@@ -64,6 +72,7 @@ describe("giveBadge", () => {
|
||||
await giveBadge(form("3", "ACH_Test"));
|
||||
|
||||
expect(state.requirePermission).toHaveBeenCalledWith(PERMS.CATALOG_EDIT);
|
||||
expect(state.tryGuardRank).toHaveBeenCalledWith(3, 7);
|
||||
expect(state.giveBadge).toHaveBeenCalledWith(3, "ACH_Test");
|
||||
expect(state.insertValues).toHaveBeenCalledWith({
|
||||
userId: 3,
|
||||
@@ -97,19 +106,42 @@ describe("giveBadge", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("does not persist a duplicate badge", async () => {
|
||||
it("does not persist a duplicate badge and skips revalidation", async () => {
|
||||
state.existingRows = [{ id: 10 }];
|
||||
|
||||
await giveBadge(form("4", "DUP"));
|
||||
|
||||
expect(state.giveBadge).toHaveBeenCalledWith(4, "DUP");
|
||||
expect(state.insertValues).not.toHaveBeenCalled();
|
||||
expect(state.revalidatePath).toHaveBeenCalledWith("/admin/badges");
|
||||
expect(state.revalidatePath).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("skips a user at or above the actor rank", async () => {
|
||||
state.tryGuardRank.mockResolvedValueOnce({ ok: false, reason: "rank" });
|
||||
|
||||
await giveBadge(form("4", "DUP"));
|
||||
|
||||
expect(state.giveBadge).not.toHaveBeenCalled();
|
||||
expect(state.insertValues).not.toHaveBeenCalled();
|
||||
expect(state.revalidatePath).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("skips an unknown user", async () => {
|
||||
state.tryGuardRank.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
reason: "not_found",
|
||||
});
|
||||
|
||||
await giveBadge(form("404", "DUP"));
|
||||
|
||||
expect(state.giveBadge).not.toHaveBeenCalled();
|
||||
expect(state.insertValues).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns early for a non-positive user id", async () => {
|
||||
await giveBadge(form("0", "X"));
|
||||
|
||||
expect(state.tryGuardRank).not.toHaveBeenCalled();
|
||||
expect(state.giveBadge).not.toHaveBeenCalled();
|
||||
expect(state.insertValues).not.toHaveBeenCalled();
|
||||
expect(state.revalidatePath).not.toHaveBeenCalled();
|
||||
@@ -138,21 +170,36 @@ describe("giveBadge", () => {
|
||||
expect(state.giveBadge).toHaveBeenCalledWith(5, "a".repeat(32));
|
||||
});
|
||||
|
||||
it("swallows persistence failures after the rcon grant", async () => {
|
||||
state.insertValues.mockRejectedValueOnce(new Error("db down"));
|
||||
it("caps bulk user and code lists", async () => {
|
||||
const users = Array.from({ length: 250 }, (_, i) => i + 1).join(",");
|
||||
const codes = Array.from({ length: 250 }, (_, i) => `C${i}`).join(",");
|
||||
|
||||
await expect(giveBadge(form("6", "BEST"))).resolves.toBeUndefined();
|
||||
await giveBadge(form(users, codes));
|
||||
|
||||
expect(state.giveBadge).toHaveBeenCalledWith(6, "BEST");
|
||||
expect(state.revalidatePath).toHaveBeenCalledWith("/admin/badges");
|
||||
// 100 users x 100 codes = 10k pairs, capped to MAX_PAIRS (500).
|
||||
const calls = state.insertValues.mock.calls.length;
|
||||
expect(calls).toBeLessThanOrEqual(500);
|
||||
});
|
||||
|
||||
it("swallows select failures and still revalidates", async () => {
|
||||
it("throws when persistence fails instead of swallowing it", async () => {
|
||||
state.insertValues.mockRejectedValueOnce(new Error("db down"));
|
||||
|
||||
await expect(giveBadge(form("6", "BEST"))).rejects.toThrow(
|
||||
"Failed to persist",
|
||||
);
|
||||
|
||||
expect(state.giveBadge).toHaveBeenCalledWith(6, "BEST");
|
||||
expect(state.revalidatePath).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("throws when the select fails instead of reporting a false success", async () => {
|
||||
state.fail = true;
|
||||
|
||||
await expect(giveBadge(form("6", "BEST"))).resolves.toBeUndefined();
|
||||
await expect(giveBadge(form("6", "BEST"))).rejects.toThrow(
|
||||
"Failed to persist",
|
||||
);
|
||||
|
||||
expect(state.revalidatePath).toHaveBeenCalledWith("/admin/badges");
|
||||
expect(state.revalidatePath).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("propagates a permission denial", async () => {
|
||||
|
||||
+92
-32
@@ -3,45 +3,105 @@
|
||||
import { and, eq, max } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { requirePermission } from "@/lib/admin/guard";
|
||||
import { tryGuardRank } from "@/lib/admin/rank-guard";
|
||||
import { db, UsersBadges } from "@/lib/db";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { rcon } from "@/lib/services/rcon";
|
||||
|
||||
// Bound the bulk surface so a paste bomb cannot turn into an unbounded N+1.
|
||||
const MAX_USERS = 100;
|
||||
const MAX_CODES = 100;
|
||||
const MAX_PAIRS = 500;
|
||||
|
||||
export async function giveBadge(formData: FormData): Promise<void> {
|
||||
await requirePermission(PERMS.CATALOG_EDIT);
|
||||
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
||||
|
||||
const userId = Number(formData.get("userId"));
|
||||
const code = String(formData.get("code") ?? "")
|
||||
.normalize("NFC")
|
||||
.trim()
|
||||
.slice(0, 32);
|
||||
if (!(userId > 0) || code.length === 0) return;
|
||||
// Support comma-separated userIds and/or codes for bulk operations
|
||||
const userIdInput = String(formData.get("userId") ?? "").trim();
|
||||
const codeInput = String(formData.get("code") ?? "").trim();
|
||||
const userIds = userIdInput
|
||||
? userIdInput
|
||||
.split(",")
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean)
|
||||
.slice(0, MAX_USERS)
|
||||
: [];
|
||||
const codes = codeInput
|
||||
? codeInput
|
||||
.split(",")
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean)
|
||||
.slice(0, MAX_CODES)
|
||||
: [];
|
||||
|
||||
// Fire the emulator command so the badge appears live for online users.
|
||||
await rcon.giveBadge(userId, code);
|
||||
|
||||
// Persist the badge directly so it survives a relog / offline grant.
|
||||
// users_badges has no unique (user_id, badge_code) constraint, so guard
|
||||
// against duplicates and compute the next free slot ourselves.
|
||||
try {
|
||||
const [existing] = await db
|
||||
.select({ id: UsersBadges.id })
|
||||
.from(UsersBadges)
|
||||
.where(
|
||||
and(eq(UsersBadges.userId, userId), eq(UsersBadges.badgeCode, code)),
|
||||
)
|
||||
.limit(1);
|
||||
if (!existing) {
|
||||
const [agg] = await db
|
||||
.select({ maxSlot: max(UsersBadges.slotId) })
|
||||
.from(UsersBadges)
|
||||
.where(eq(UsersBadges.userId, userId));
|
||||
const slotId = (agg?.maxSlot ?? 0) + 1;
|
||||
await db.insert(UsersBadges).values({ userId, slotId, badgeCode: code });
|
||||
}
|
||||
} catch {
|
||||
// Best-effort: the RCON grant already succeeded for online users.
|
||||
if (userIds.length === 0 || codes.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
revalidatePath("/admin/badges");
|
||||
let granted = 0;
|
||||
let failures = 0;
|
||||
for (const rawUserId of userIds) {
|
||||
const userId = Number(rawUserId);
|
||||
if (!(userId > 0)) continue;
|
||||
|
||||
// Never hand badges to a target at or above the actor's rank.
|
||||
const guard = await tryGuardRank(userId, staff.rank);
|
||||
if (!guard.ok) continue;
|
||||
|
||||
for (const rawCode of codes) {
|
||||
if (granted + failures >= MAX_PAIRS) break;
|
||||
// Truncate badge code to 32 characters.
|
||||
const truncatedCode = rawCode.slice(0, 32);
|
||||
if (truncatedCode.length === 0) continue;
|
||||
|
||||
// Fire the emulator command so the badge appears live for online users.
|
||||
// Offline/unreachable emulator must not block the DB persistence.
|
||||
try {
|
||||
await rcon.giveBadge(userId, truncatedCode);
|
||||
} catch {
|
||||
// ignore rcon errors per pair
|
||||
}
|
||||
|
||||
// Persist the badge directly so it survives a relog / offline grant.
|
||||
// users_badges has no unique (user_id, badge_code) constraint, so guard
|
||||
// against duplicates and compute the next free slot ourselves.
|
||||
try {
|
||||
const [existing] = await db
|
||||
.select({ id: UsersBadges.id })
|
||||
.from(UsersBadges)
|
||||
.where(
|
||||
and(
|
||||
eq(UsersBadges.userId, userId),
|
||||
eq(UsersBadges.badgeCode, truncatedCode),
|
||||
),
|
||||
)
|
||||
.limit(1);
|
||||
if (!existing) {
|
||||
const [agg] = await db
|
||||
.select({ maxSlot: max(UsersBadges.slotId) })
|
||||
.from(UsersBadges)
|
||||
.where(eq(UsersBadges.userId, userId));
|
||||
const slotId = (agg?.maxSlot ?? 0) + 1;
|
||||
await db.insert(UsersBadges).values({
|
||||
userId,
|
||||
slotId,
|
||||
badgeCode: truncatedCode,
|
||||
});
|
||||
granted++;
|
||||
}
|
||||
} catch {
|
||||
// A persistence failure must be visible, not silently dropped.
|
||||
failures++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failures > 0) {
|
||||
throw new Error(
|
||||
`Failed to persist ${failures} badge grant(s); the emulator may have succeeded for some users`,
|
||||
);
|
||||
}
|
||||
if (granted > 0) {
|
||||
revalidatePath("/admin/badges");
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ vi.mock("@/lib/auth", () => ({
|
||||
signOut: vi.fn(),
|
||||
}));
|
||||
|
||||
import { Ban, db, User } from "@/lib/db";
|
||||
import { Ban, db } from "@/lib/db";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { createBan, liftBan } from "./admin-bans";
|
||||
|
||||
@@ -42,6 +42,20 @@ vi.mock("@/lib/admin/guard", () => ({
|
||||
requirePermissionRateLimited: mockRequirePermissionRateLimited,
|
||||
}));
|
||||
|
||||
const mockTryGuardRank = vi.hoisted(() => vi.fn());
|
||||
vi.mock("@/lib/admin/rank-guard", () => ({
|
||||
tryGuardRank: mockTryGuardRank,
|
||||
}));
|
||||
|
||||
const mockRedirect = vi.hoisted(() =>
|
||||
vi.fn(() => {
|
||||
throw new Error("NEXT_REDIRECT");
|
||||
}),
|
||||
);
|
||||
vi.mock("next/navigation", () => ({
|
||||
redirect: mockRedirect,
|
||||
}));
|
||||
|
||||
// Hoisted mocks for internal db functions
|
||||
const hoistedInsertValues = vi.hoisted(() => vi.fn());
|
||||
const hoistedDeleteWhere = vi.hoisted(() => vi.fn());
|
||||
@@ -61,7 +75,6 @@ vi.mock("@/lib/db", () => {
|
||||
select: vi.fn(() => ({ from: hoistedSelectFrom })),
|
||||
},
|
||||
Ban: { id: "Ban.id", userId: "Ban.userId" },
|
||||
User: { id: "User.id", username: "User.username" },
|
||||
};
|
||||
});
|
||||
|
||||
@@ -93,7 +106,11 @@ beforeEach(() => {
|
||||
mockRequirePermissionRateLimited.mockResolvedValue({ id: 100, rank: 7 });
|
||||
hoistedInsertValues.mockResolvedValue([{ insertId: 1 }]);
|
||||
hoistedDeleteWhere.mockResolvedValue([{ affectedRows: 1 }]);
|
||||
hoistedSelectLimit.mockResolvedValue([{ username: "testuser" }]);
|
||||
hoistedSelectLimit.mockResolvedValue([{ userId: 5 }]);
|
||||
mockTryGuardRank.mockResolvedValue({
|
||||
ok: true,
|
||||
target: { username: "testuser", rank: 1, mail: null },
|
||||
});
|
||||
mockHeaders.mockReturnValue(new Map());
|
||||
});
|
||||
|
||||
@@ -110,6 +127,7 @@ describe("createBan", () => {
|
||||
expect(mockRequirePermissionRateLimited).toHaveBeenCalledWith(
|
||||
PERMS.USERS_BAN,
|
||||
);
|
||||
expect(mockTryGuardRank).toHaveBeenCalledWith(1, 7);
|
||||
expect(db.insert).toHaveBeenCalledWith(Ban);
|
||||
expect(hoistedInsertValues).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
@@ -156,6 +174,7 @@ describe("createBan", () => {
|
||||
|
||||
await createBan(formData);
|
||||
|
||||
expect(mockTryGuardRank).not.toHaveBeenCalled();
|
||||
expect(db.insert).not.toHaveBeenCalled();
|
||||
expect(mockLogStaffActivity).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -169,27 +188,45 @@ describe("createBan", () => {
|
||||
|
||||
await createBan(formData);
|
||||
|
||||
expect(mockTryGuardRank).not.toHaveBeenCalled();
|
||||
expect(db.insert).not.toHaveBeenCalled();
|
||||
expect(mockLogStaffActivity).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("disconnects user via RCON if user found", async () => {
|
||||
it("disconnects user via RCON using the guarded username", async () => {
|
||||
mockTryGuardRank.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
target: { username: "anotheruser", rank: 1, mail: null },
|
||||
});
|
||||
const formData = new FormData();
|
||||
formData.append("userId", "3");
|
||||
formData.append("type", "account");
|
||||
formData.append("reason", "Test reason");
|
||||
|
||||
hoistedSelectLimit.mockResolvedValueOnce([{ username: "anotheruser" }]);
|
||||
|
||||
await createBan(formData);
|
||||
|
||||
expect(mockTryGuardRank).toHaveBeenCalledWith(3, 7);
|
||||
expect(mockRconDisconnectUser).toHaveBeenCalledWith(3, "anotheruser");
|
||||
expect(mockEq).toHaveBeenCalledWith(User.id, 3);
|
||||
});
|
||||
|
||||
it("refuses to ban a user with equal or higher rank", async () => {
|
||||
mockTryGuardRank.mockResolvedValueOnce({ ok: false, reason: "rank" });
|
||||
const formData = new FormData();
|
||||
formData.append("userId", "3");
|
||||
formData.append("type", "account");
|
||||
formData.append("reason", "Test reason");
|
||||
|
||||
await expect(createBan(formData)).rejects.toThrow("NEXT_REDIRECT");
|
||||
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/bans?error=rank");
|
||||
expect(db.insert).not.toHaveBeenCalled();
|
||||
expect(mockLogStaffActivity).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("liftBan", () => {
|
||||
it("lifts an existing ban and logs activity", async () => {
|
||||
hoistedSelectLimit.mockResolvedValueOnce([{ userId: 5 }]);
|
||||
const formData = new FormData();
|
||||
formData.append("id", "123");
|
||||
|
||||
@@ -198,6 +235,8 @@ describe("liftBan", () => {
|
||||
expect(mockRequirePermissionRateLimited).toHaveBeenCalledWith(
|
||||
PERMS.USERS_BAN,
|
||||
);
|
||||
expect(mockEq).toHaveBeenCalledWith(Ban.id, 123);
|
||||
expect(mockTryGuardRank).toHaveBeenCalledWith(5, 7);
|
||||
expect(db.delete).toHaveBeenCalledWith(Ban);
|
||||
expect(hoistedDeleteWhere).toHaveBeenCalledWith(mockEq(Ban.id, 123));
|
||||
expect(mockLogStaffActivity).toHaveBeenCalledWith(
|
||||
@@ -209,12 +248,37 @@ describe("liftBan", () => {
|
||||
expect(mockRevalidatePath).toHaveBeenCalledWith("/admin/bans");
|
||||
});
|
||||
|
||||
it("refuses to lift a ban placed on an equal or higher rank user", async () => {
|
||||
hoistedSelectLimit.mockResolvedValueOnce([{ userId: 5 }]);
|
||||
mockTryGuardRank.mockResolvedValueOnce({ ok: false, reason: "rank" });
|
||||
const formData = new FormData();
|
||||
formData.append("id", "123");
|
||||
|
||||
await expect(liftBan(formData)).rejects.toThrow("NEXT_REDIRECT");
|
||||
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/bans?error=rank");
|
||||
expect(hoistedDeleteWhere).not.toHaveBeenCalled();
|
||||
expect(mockLogStaffActivity).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("tolerates a lift when the targeted user no longer exists", async () => {
|
||||
hoistedSelectLimit.mockResolvedValueOnce([{ userId: 5 }]);
|
||||
mockTryGuardRank.mockResolvedValueOnce({ ok: false, reason: "not_found" });
|
||||
const formData = new FormData();
|
||||
formData.append("id", "123");
|
||||
|
||||
await liftBan(formData);
|
||||
|
||||
expect(db.delete).toHaveBeenCalledWith(Ban);
|
||||
});
|
||||
|
||||
it("does not delete if ban id is not positive", async () => {
|
||||
const formData = new FormData();
|
||||
formData.append("id", "0");
|
||||
|
||||
await liftBan(formData);
|
||||
|
||||
expect(mockEq).not.toHaveBeenCalled();
|
||||
expect(db.delete).not.toHaveBeenCalled();
|
||||
expect(hoistedDeleteWhere).not.toHaveBeenCalled();
|
||||
expect(mockLogStaffActivity).not.toHaveBeenCalled();
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { redirect } from "next/navigation";
|
||||
import { requirePermissionRateLimited } from "@/lib/admin/guard";
|
||||
import { Ban, db, User } from "@/lib/db";
|
||||
import { tryGuardRank } from "@/lib/admin/rank-guard";
|
||||
import { Ban, db } from "@/lib/db";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { rcon } from "@/lib/services/rcon";
|
||||
import { logStaffActivity } from "@/lib/services/staff-activity";
|
||||
@@ -26,16 +28,15 @@ export async function createBan(formData: FormData): Promise<void> {
|
||||
const type = String(formData.get("type"));
|
||||
if (!(userId > 0) || !BAN_TYPES.has(type)) return;
|
||||
|
||||
const guard = await tryGuardRank(userId, staff.rank);
|
||||
if (!guard.ok && guard.reason === "rank") {
|
||||
redirect("/admin/bans?error=rank");
|
||||
}
|
||||
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
// Emulator convention: banExpire 0 = permanent (not a far-future timestamp).
|
||||
const banExpire = hours > 0 ? now + Math.floor(hours) * 3600 : 0;
|
||||
|
||||
const [user] = await db
|
||||
.select({ username: User.username })
|
||||
.from(User)
|
||||
.where(eq(User.id, userId))
|
||||
.limit(1);
|
||||
|
||||
await db.insert(Ban).values({
|
||||
userId,
|
||||
ip: "",
|
||||
@@ -48,7 +49,7 @@ export async function createBan(formData: FormData): Promise<void> {
|
||||
cfhTopic: -1,
|
||||
});
|
||||
|
||||
if (user) await rcon.disconnectUser(userId, user.username);
|
||||
if (guard.ok) await rcon.disconnectUser(userId, guard.target.username);
|
||||
await logStaffActivity({
|
||||
staffId: staff.id,
|
||||
action: "user_ban",
|
||||
@@ -63,6 +64,17 @@ export async function liftBan(formData: FormData): Promise<void> {
|
||||
const staff = await requirePermissionRateLimited(PERMS.USERS_BAN);
|
||||
const id = Number(formData.get("id"));
|
||||
if (id > 0) {
|
||||
const [ban] = await db
|
||||
.select({ userId: Ban.userId })
|
||||
.from(Ban)
|
||||
.where(eq(Ban.id, id))
|
||||
.limit(1);
|
||||
if (ban) {
|
||||
const guard = await tryGuardRank(ban.userId, staff.rank);
|
||||
if (!guard.ok && guard.reason === "rank") {
|
||||
redirect("/admin/bans?error=rank");
|
||||
}
|
||||
}
|
||||
await db.delete(Ban).where(eq(Ban.id, id));
|
||||
await logStaffActivity({
|
||||
staffId: staff.id,
|
||||
|
||||
@@ -70,7 +70,7 @@ export const liftBanFromHelpTicket = adminAction(
|
||||
.where(eq(WebsiteHelpCenterTickets.id, ticketId));
|
||||
}
|
||||
|
||||
logAudit({
|
||||
await logAudit({
|
||||
userId: ctx.session.user.id,
|
||||
action: "unban_via_help_ticket",
|
||||
target: "User",
|
||||
@@ -123,7 +123,7 @@ export const replyHelpCenterTicket = adminAction(
|
||||
.where(eq(WebsiteHelpCenterTickets.id, ticketId));
|
||||
});
|
||||
|
||||
logAudit({
|
||||
await logAudit({
|
||||
userId: staffId,
|
||||
action: "help_center_ticket_reply",
|
||||
target: "WebsiteHelpCenterTickets",
|
||||
@@ -157,7 +157,7 @@ export const closeHelpCenterTicket = adminAction(
|
||||
.set({ open: false, updatedAt: now })
|
||||
.where(eq(WebsiteHelpCenterTickets.id, ticketId));
|
||||
|
||||
logAudit({
|
||||
await logAudit({
|
||||
userId: Number(ctx.session.user.id),
|
||||
action: "help_center_ticket_close",
|
||||
target: "WebsiteHelpCenterTickets",
|
||||
@@ -193,7 +193,7 @@ export const reopenHelpCenterTicket = adminAction(
|
||||
.set({ open: true, updatedAt: now })
|
||||
.where(eq(WebsiteHelpCenterTickets.id, ticketId));
|
||||
|
||||
logAudit({
|
||||
await logAudit({
|
||||
userId: Number(ctx.session.user.id),
|
||||
action: "help_center_ticket_reopen",
|
||||
target: "WebsiteHelpCenterTickets",
|
||||
|
||||
+30
-34
@@ -4,11 +4,32 @@ import { mkdir, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { requirePermission } from "@/lib/admin/guard";
|
||||
import { validateSiteImageUpload } from "@/lib/images/site-image-upload";
|
||||
import { MEDIA_ROOT, resolveMediaPath } from "@/lib/media-storage";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
|
||||
const MAX_SIZE = 5 * 1024 * 1024; // 5MB
|
||||
const ALLOWED = ["image/png", "image/jpeg", "image/gif", "image/webp"];
|
||||
/**
|
||||
* Store an uploaded media file under MEDIA_ROOT.
|
||||
*
|
||||
* The extension always comes from the *detected* format (magic bytes + a full
|
||||
* sharp decode), never from `file.name` or the browser-supplied MIME type:
|
||||
* trusting either lets arbitrary bytes land on disk with an attacker-chosen name
|
||||
* that the media route would then serve.
|
||||
*/
|
||||
async function storeUploadedMedia(
|
||||
file: File,
|
||||
): Promise<{ ok: true; name: string } | { ok: false; error: string }> {
|
||||
const validated = await validateSiteImageUpload(file);
|
||||
if (!validated.success) return { ok: false, error: validated.error };
|
||||
const baseDir = MEDIA_ROOT;
|
||||
await mkdir(baseDir, { recursive: true });
|
||||
const name = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}.${validated.extension}`;
|
||||
const filePath = resolveMediaPath(name);
|
||||
if (!filePath.startsWith(baseDir + path.sep))
|
||||
return { ok: false, error: "Invalid path" };
|
||||
await writeFile(filePath, validated.bytes);
|
||||
return { ok: true, name };
|
||||
}
|
||||
|
||||
export async function uploadMedia(
|
||||
formData: FormData,
|
||||
@@ -16,25 +37,9 @@ export async function uploadMedia(
|
||||
await requirePermission(PERMS.PAGES_EDIT);
|
||||
const file = formData.get("file") as File | null;
|
||||
if (!file || file.size === 0) return { ok: false, error: "No file provided" };
|
||||
if (file.size > MAX_SIZE)
|
||||
return { ok: false, error: "File too large (max 5MB)" };
|
||||
if (!ALLOWED.includes(file.type))
|
||||
return {
|
||||
ok: false,
|
||||
error: "Invalid file type. Allowed: PNG, JPEG, GIF, WebP",
|
||||
};
|
||||
|
||||
const baseDir = MEDIA_ROOT;
|
||||
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
||||
await mkdir(baseDir, { recursive: true });
|
||||
|
||||
const ext = file.name.split(".").pop() ?? "png";
|
||||
const name = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}.${ext}`;
|
||||
const bytes = await file.arrayBuffer();
|
||||
const filePath = resolveMediaPath(name);
|
||||
if (!filePath.startsWith(baseDir + path.sep)) throw new Error("Invalid path");
|
||||
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
||||
await writeFile(filePath, Buffer.from(bytes));
|
||||
const stored = await storeUploadedMedia(file);
|
||||
if (!stored.ok) return { ok: false, error: stored.error };
|
||||
|
||||
revalidatePath("/api/media");
|
||||
revalidatePath("/admin/media");
|
||||
@@ -45,6 +50,8 @@ export async function deleteMedia(name: string): Promise<void> {
|
||||
await requirePermission(PERMS.PAGES_EDIT);
|
||||
const { unlink } = await import("node:fs/promises");
|
||||
const baseDir = MEDIA_ROOT;
|
||||
// A name that is not a bare file name never reaches the unlink.
|
||||
if (name.includes("/") || name.includes("\\") || name.includes("..")) return;
|
||||
const filePath = resolveMediaPath(name);
|
||||
if (!filePath.startsWith(baseDir + path.sep)) return;
|
||||
try {
|
||||
@@ -62,22 +69,11 @@ export async function uploadMediaAndReturn(
|
||||
await requirePermission(PERMS.PAGES_EDIT);
|
||||
const file = formData.get("file") as File | null;
|
||||
if (!file || file.size === 0) return "";
|
||||
if (file.size > MAX_SIZE) return "";
|
||||
if (!ALLOWED.includes(file.type)) return "";
|
||||
|
||||
const baseDir = MEDIA_ROOT;
|
||||
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
||||
await mkdir(baseDir, { recursive: true });
|
||||
|
||||
const ext = file.name.split(".").pop() ?? "png";
|
||||
const name = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}.${ext}`;
|
||||
const bytes = await file.arrayBuffer();
|
||||
const filePath = resolveMediaPath(name);
|
||||
if (!filePath.startsWith(baseDir + path.sep)) return "";
|
||||
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
||||
await writeFile(filePath, Buffer.from(bytes));
|
||||
const stored = await storeUploadedMedia(file);
|
||||
if (!stored.ok) return "";
|
||||
|
||||
revalidatePath("/api/media");
|
||||
revalidatePath("/admin/media");
|
||||
return `/api/media/${name}`;
|
||||
return `/api/media/${stored.name}`;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { requirePermission } from "@/lib/admin/guard";
|
||||
import { db, WebsiteRareValueCategories, WebsiteRareValues } from "@/lib/db";
|
||||
import { EDGE_CACHE_TAGS, purgeEdgeCache } from "@/lib/edge-cache";
|
||||
import { formPositiveBigInt } from "@/lib/form-data";
|
||||
import { logger } from "@/lib/logger";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
|
||||
export async function createCategory(formData: FormData): Promise<void> {
|
||||
@@ -31,8 +32,9 @@ export async function createCategory(formData: FormData): Promise<void> {
|
||||
badge,
|
||||
priority,
|
||||
});
|
||||
} catch {
|
||||
// Unique name collision or DB error — ignore, page will re-render unchanged.
|
||||
} catch (err) {
|
||||
// Unique name collision or DB error — page re-renders unchanged.
|
||||
logger.error("Failed to create rare-value category", { err });
|
||||
}
|
||||
revalidatePath("/admin/rare-values");
|
||||
void purgeEdgeCache([EDGE_CACHE_TAGS.public], "rare values edited");
|
||||
@@ -51,8 +53,8 @@ export async function deleteCategory(formData: FormData): Promise<void> {
|
||||
await db
|
||||
.delete(WebsiteRareValueCategories)
|
||||
.where(eq(WebsiteRareValueCategories.id, id));
|
||||
} catch {
|
||||
// Not found or DB error — ignore.
|
||||
} catch (err) {
|
||||
logger.error("Failed to delete rare value", { err });
|
||||
}
|
||||
revalidatePath("/admin/rare-values");
|
||||
void purgeEdgeCache([EDGE_CACHE_TAGS.public], "rare values edited");
|
||||
@@ -100,8 +102,8 @@ export async function createValue(formData: FormData): Promise<void> {
|
||||
currencyType,
|
||||
furnitureIcon,
|
||||
});
|
||||
} catch {
|
||||
// DB error — ignore.
|
||||
} catch (err) {
|
||||
logger.error("Failed to persist rare value", { err });
|
||||
}
|
||||
revalidatePath("/admin/rare-values");
|
||||
void purgeEdgeCache([EDGE_CACHE_TAGS.public], "rare values edited");
|
||||
@@ -114,8 +116,8 @@ export async function deleteValue(formData: FormData): Promise<void> {
|
||||
|
||||
try {
|
||||
await db.delete(WebsiteRareValues).where(eq(WebsiteRareValues.id, id));
|
||||
} catch {
|
||||
// Not found or DB error — ignore.
|
||||
} catch (err) {
|
||||
logger.error("Failed to delete rare value", { err });
|
||||
}
|
||||
revalidatePath("/admin/rare-values");
|
||||
void purgeEdgeCache([EDGE_CACHE_TAGS.public], "rare values edited");
|
||||
@@ -146,8 +148,8 @@ export async function updateCategory(formData: FormData): Promise<void> {
|
||||
.update(WebsiteRareValueCategories)
|
||||
.set({ name, badge, priority })
|
||||
.where(eq(WebsiteRareValueCategories.id, id));
|
||||
} catch {
|
||||
// Unique name collision or DB error — ignore.
|
||||
} catch (err) {
|
||||
logger.error("Failed to update rare-value category", { err });
|
||||
}
|
||||
revalidatePath("/admin/rare-values");
|
||||
void purgeEdgeCache([EDGE_CACHE_TAGS.public], "rare values edited");
|
||||
@@ -201,8 +203,8 @@ export async function updateValue(formData: FormData): Promise<void> {
|
||||
.update(WebsiteRareValues)
|
||||
.set(sets)
|
||||
.where(eq(WebsiteRareValues.id, id));
|
||||
} catch {
|
||||
// DB error — ignore.
|
||||
} catch (err) {
|
||||
logger.error("Failed to persist rare value", { err });
|
||||
}
|
||||
revalidatePath("/admin/rare-values");
|
||||
void purgeEdgeCache([EDGE_CACHE_TAGS.public], "rare values edited");
|
||||
|
||||
@@ -14,10 +14,19 @@ import {
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { clearOfficialHabboFurnidataCache } from "@/lib/services/habbo-furnidata-cache";
|
||||
import { clearBadgeCache } from "@/lib/services/habboassets";
|
||||
import {
|
||||
isSecretSettingKey,
|
||||
SECRET_PLACEHOLDER,
|
||||
} from "@/lib/services/setting-secrets";
|
||||
import { siteSettings } from "@/lib/services/site-settings";
|
||||
|
||||
const managedKeySet = new Set(MANAGED_SETTING_KEYS);
|
||||
|
||||
// Raw keys only the CMS core is allowed to own. Writing an arbitrary key from
|
||||
// the generic "advanced key/value" form previously meant a staff member could
|
||||
// overwrite `turnstile_secret`, `force_staff_2fa` or `min_staff_rank`.
|
||||
const RAW_SETTING_KEY_RE = /^[a-z0-9][a-z0-9_.-]{0,127}$/;
|
||||
|
||||
function normalizeSettingValue(key: string, value: string): string {
|
||||
if (key === HABBO_GAMEDATA_HOTEL_SETTING_KEY) {
|
||||
return normalizeHabboGamedataHotel(value);
|
||||
@@ -71,11 +80,12 @@ export async function updateSetting(formData: FormData): Promise<void> {
|
||||
const key = String(formData.get("key") ?? "")
|
||||
.normalize("NFC")
|
||||
.trim();
|
||||
const value = normalizeSettingValue(
|
||||
key,
|
||||
String(formData.get("value") ?? "").normalize("NFC"),
|
||||
);
|
||||
if (!key) return;
|
||||
const raw = String(formData.get("value") ?? "").normalize("NFC");
|
||||
if (!key || !RAW_SETTING_KEY_RE.test(key)) return;
|
||||
// Blank on a secret means "keep what is stored", so the UI can render a
|
||||
// placeholder without the risk of wiping the credential.
|
||||
if (isSecretSettingKey(key) && raw === SECRET_PLACEHOLDER) return;
|
||||
const value = isSecretSettingKey(key) ? raw : normalizeSettingValue(key, raw);
|
||||
await db
|
||||
.insert(WebsiteSetting)
|
||||
.values({ key, value })
|
||||
@@ -90,7 +100,7 @@ export async function createSetting(formData: FormData): Promise<void> {
|
||||
const key = String(formData.get("key") ?? "")
|
||||
.normalize("NFC")
|
||||
.trim()
|
||||
.slice(0, 255);
|
||||
.slice(0, 128);
|
||||
const value = normalizeSettingValue(
|
||||
key,
|
||||
String(formData.get("value") ?? "").normalize("NFC"),
|
||||
@@ -99,7 +109,17 @@ export async function createSetting(formData: FormData): Promise<void> {
|
||||
.normalize("NFC")
|
||||
.trim()
|
||||
.slice(0, 255);
|
||||
if (!key) return;
|
||||
// Managed keys go through `saveManagedSettings`; anything else must be a
|
||||
// clearly namespaced custom key, and lockout/security settings are never
|
||||
// writable through the free-form form.
|
||||
if (!key || !RAW_SETTING_KEY_RE.test(key)) return;
|
||||
if (
|
||||
key === "force_staff_2fa" ||
|
||||
key === "min_staff_rank" ||
|
||||
key === "maintenance_enabled"
|
||||
) {
|
||||
return;
|
||||
}
|
||||
await db
|
||||
.insert(WebsiteSetting)
|
||||
.values({ key, value, comment: comment || null })
|
||||
|
||||
@@ -141,13 +141,13 @@ describe("saveTheme", () => {
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?saved=1");
|
||||
});
|
||||
|
||||
it("fails soft when a write fails and still redirects", async () => {
|
||||
it("redirects with an error when a write fails", async () => {
|
||||
fakeDbConfig.fail = (kind: string) => kind === "insert";
|
||||
await saveTheme(form({ color_primary: "#123456" }));
|
||||
expect(mockReload).not.toHaveBeenCalled();
|
||||
expect(mockLogStaffActivity).not.toHaveBeenCalled();
|
||||
expect(mockRevalidatePath).not.toHaveBeenCalled();
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?saved=1");
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?error=1");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -182,12 +182,12 @@ describe("applyPreset", () => {
|
||||
expect(mockLogStaffActivity).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fails soft on write errors", async () => {
|
||||
it("redirects with an error on write errors", async () => {
|
||||
fakeDbConfig.fail = (kind: string) => kind === "insert";
|
||||
await applyPreset(form({ preset: "Ocean" }));
|
||||
expect(mockReload).not.toHaveBeenCalled();
|
||||
expect(mockLogStaffActivity).not.toHaveBeenCalled();
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?preset=Ocean");
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?error=1");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -218,12 +218,12 @@ describe("saveCustomTheme", () => {
|
||||
expect(mockSnapshotCurrentTheme).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fails soft on persist errors", async () => {
|
||||
it("redirects with an error on persist errors", async () => {
|
||||
mockUpsertCustomTheme.mockRejectedValueOnce(new Error("store down"));
|
||||
await saveCustomTheme(form({ name: "My Theme" }));
|
||||
expect(mockLogStaffActivity).not.toHaveBeenCalled();
|
||||
expect(mockRevalidatePath).not.toHaveBeenCalled();
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?savedTheme=1");
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?error=1");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -284,7 +284,7 @@ describe("applyCustomTheme", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("fails soft on write errors", async () => {
|
||||
it("redirects with an error on write errors", async () => {
|
||||
mockGetCustomTheme.mockResolvedValue({
|
||||
id: "t1",
|
||||
name: "Studio",
|
||||
@@ -296,7 +296,7 @@ describe("applyCustomTheme", () => {
|
||||
expect(mockReload).not.toHaveBeenCalled();
|
||||
expect(mockLogStaffActivity).not.toHaveBeenCalled();
|
||||
expect(mockRevalidatePath).not.toHaveBeenCalled();
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?theme=Studio");
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?error=1");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -324,11 +324,11 @@ describe("renameCustomTheme", () => {
|
||||
expect(mockSnapshotCurrentTheme).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fails soft on upsert errors", async () => {
|
||||
it("redirects with an error on upsert errors", async () => {
|
||||
mockUpsertCustomTheme.mockRejectedValueOnce(new Error("store down"));
|
||||
await renameCustomTheme(form({ id: "t1", name: "Renamed" }));
|
||||
expect(mockRevalidatePath).not.toHaveBeenCalled();
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?renamed=1");
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?error=1");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -350,10 +350,10 @@ describe("deleteCustomTheme", () => {
|
||||
expect(mockDeleteCustomThemeStore).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fails soft on store errors", async () => {
|
||||
it("redirects with an error on store errors", async () => {
|
||||
mockDeleteCustomThemeStore.mockRejectedValueOnce(new Error("store down"));
|
||||
await deleteCustomTheme(form({ id: "t1" }));
|
||||
expect(mockRevalidatePath).not.toHaveBeenCalled();
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?deletedTheme=1");
|
||||
expect(mockRedirect).toHaveBeenCalledWith("/admin/theme?error=1");
|
||||
});
|
||||
});
|
||||
@@ -104,7 +104,7 @@ export async function saveTheme(formData: FormData): Promise<void> {
|
||||
});
|
||||
revalidatePath("/", "layout");
|
||||
} catch {
|
||||
// ignore — page re-renders current state
|
||||
return redirect("/admin/theme?error=1");
|
||||
}
|
||||
redirect("/admin/theme?saved=1");
|
||||
}
|
||||
@@ -128,7 +128,7 @@ export async function applyPreset(formData: FormData): Promise<void> {
|
||||
});
|
||||
revalidatePath("/", "layout");
|
||||
} catch {
|
||||
// ignore
|
||||
return redirect("/admin/theme?error=1");
|
||||
}
|
||||
redirect(`/admin/theme?preset=${encodeURIComponent(name)}`);
|
||||
}
|
||||
@@ -149,7 +149,7 @@ export async function saveCustomTheme(formData: FormData): Promise<void> {
|
||||
});
|
||||
revalidatePath("/admin/theme");
|
||||
} catch {
|
||||
// ignore
|
||||
return redirect("/admin/theme?error=1");
|
||||
}
|
||||
redirect("/admin/theme?savedTheme=1");
|
||||
}
|
||||
@@ -175,7 +175,7 @@ export async function applyCustomTheme(formData: FormData): Promise<void> {
|
||||
});
|
||||
revalidatePath("/", "layout");
|
||||
} catch {
|
||||
// ignore
|
||||
return redirect("/admin/theme?error=1");
|
||||
}
|
||||
redirect(`/admin/theme?theme=${encodeURIComponent(theme.name)}`);
|
||||
}
|
||||
@@ -194,7 +194,7 @@ export async function renameCustomTheme(formData: FormData): Promise<void> {
|
||||
await upsertCustomTheme(name, snapshot, id);
|
||||
revalidatePath("/admin/theme");
|
||||
} catch {
|
||||
// ignore
|
||||
return redirect("/admin/theme?error=1");
|
||||
}
|
||||
redirect("/admin/theme?renamed=1");
|
||||
}
|
||||
@@ -209,7 +209,7 @@ export async function deleteCustomTheme(formData: FormData): Promise<void> {
|
||||
await deleteCustomThemeStore(id);
|
||||
revalidatePath("/admin/theme");
|
||||
} catch {
|
||||
// ignore
|
||||
return redirect("/admin/theme?error=1");
|
||||
}
|
||||
redirect("/admin/theme?deletedTheme=1");
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { eq } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { requirePermission } from "@/lib/admin/guard";
|
||||
import { db, WebsiteWriteableBoxes } from "@/lib/db";
|
||||
import { logger } from "@/lib/logger";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { logStaffActivity } from "@/lib/services/staff-activity";
|
||||
|
||||
@@ -72,8 +73,9 @@ export async function createBox(formData: FormData): Promise<void> {
|
||||
targetType: "writeable_box",
|
||||
targetId: Number(result.insertId),
|
||||
});
|
||||
} catch {
|
||||
// DB unavailable — swallow and re-render.
|
||||
} catch (err) {
|
||||
// DB unavailable — keep the page usable, but record the failure.
|
||||
logger.error("Failed to create writeable box", { err });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -116,7 +118,8 @@ export async function updateBox(formData: FormData): Promise<void> {
|
||||
targetType: "writeable_box",
|
||||
targetId: Number(id),
|
||||
});
|
||||
} catch {
|
||||
} catch (err) {
|
||||
logger.error("Failed to persist writeable box", { err });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -140,7 +143,8 @@ export async function deleteBox(formData: FormData): Promise<void> {
|
||||
targetType: "writeable_box",
|
||||
targetId: Number(id),
|
||||
});
|
||||
} catch {
|
||||
} catch (err) {
|
||||
logger.error("Failed to persist writeable box", { err });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -168,7 +172,8 @@ export async function toggleBox(formData: FormData): Promise<void> {
|
||||
targetType: "writeable_box",
|
||||
targetId: Number(id),
|
||||
});
|
||||
} catch {
|
||||
} catch (err) {
|
||||
logger.error("Failed to persist writeable box", { err });
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,9 @@ const core = vi.hoisted(() => ({
|
||||
.trim(),
|
||||
password: String(password ?? "").normalize("NFC"),
|
||||
}),
|
||||
isLoginLocked: vi.fn(async () => false),
|
||||
recordLoginFailure: vi.fn(async () => false),
|
||||
clearLoginLockout: vi.fn(async () => undefined),
|
||||
}));
|
||||
|
||||
vi.mock("@/env", () => ({ env: {} }));
|
||||
@@ -27,8 +30,14 @@ vi.mock("@/lib/services/captcha", () => ({
|
||||
vi.mock("@/lib/services/site-settings", () => ({
|
||||
siteSettings: { getBool: vi.fn() },
|
||||
}));
|
||||
vi.mock("@/lib/auth/login-lockout", () => ({
|
||||
isLoginLocked: core.isLoginLocked,
|
||||
recordLoginFailure: core.recordLoginFailure,
|
||||
clearLoginLockout: core.clearLoginLockout,
|
||||
}));
|
||||
|
||||
const user = (overrides = {}) => ({
|
||||
id: 42,
|
||||
password: "hash",
|
||||
twoFactorConfirmedAt: null,
|
||||
mail: null,
|
||||
@@ -45,6 +54,9 @@ beforeEach(() => {
|
||||
core.verifyLoginPassword.mockResolvedValue({ valid: true });
|
||||
core.isEmailUnverified.mockResolvedValue(false);
|
||||
core.runDummyHashCheck.mockResolvedValue(undefined);
|
||||
core.isLoginLocked.mockResolvedValue(false);
|
||||
core.recordLoginFailure.mockResolvedValue(false);
|
||||
core.clearLoginLockout.mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
describe("precheckLogin", () => {
|
||||
@@ -85,4 +97,57 @@ describe("precheckLogin", () => {
|
||||
core.isEmailUnverified.mockResolvedValue(true);
|
||||
expect(await precheckLogin("user", "pass")).toBe("unverified");
|
||||
});
|
||||
|
||||
it("returns locked for an account that is already locked out", async () => {
|
||||
core.getLoginUser.mockResolvedValue(user());
|
||||
core.isLoginLocked.mockResolvedValue(true);
|
||||
expect(await precheckLogin("user", "pass")).toBe("locked");
|
||||
// The password is never verified while locked, so a correct password
|
||||
// cannot walk a locked account back in.
|
||||
expect(core.verifyLoginPassword).not.toHaveBeenCalled();
|
||||
expect(core.clearLoginLockout).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("checks the lockout before verifying the password", async () => {
|
||||
core.getLoginUser.mockResolvedValue(user());
|
||||
const order: string[] = [];
|
||||
core.getLoginUser.mockImplementation(async () => {
|
||||
order.push("lookup");
|
||||
return user();
|
||||
});
|
||||
core.isLoginLocked.mockImplementation(async () => {
|
||||
order.push("lock");
|
||||
return false;
|
||||
});
|
||||
core.verifyLoginPassword.mockImplementation(async () => {
|
||||
order.push("verify");
|
||||
return { valid: true };
|
||||
});
|
||||
expect(await precheckLogin("user", "pass")).toBe("ok");
|
||||
expect(order).toEqual(["lookup", "lock", "verify"]);
|
||||
});
|
||||
|
||||
it("records a failure and skips the clear when the password is wrong", async () => {
|
||||
core.getLoginUser.mockResolvedValue(user());
|
||||
core.verifyLoginPassword.mockResolvedValue({ valid: false });
|
||||
core.recordLoginFailure.mockResolvedValue(false);
|
||||
expect(await precheckLogin("user", "pass")).toBe("invalid");
|
||||
expect(core.recordLoginFailure).toHaveBeenCalledWith(42);
|
||||
expect(core.clearLoginLockout).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("clears the lockout after a successful authentication", async () => {
|
||||
core.getLoginUser.mockResolvedValue(user());
|
||||
expect(await precheckLogin("user", "pass")).toBe("ok");
|
||||
expect(core.clearLoginLockout).toHaveBeenCalledWith(42);
|
||||
expect(core.recordLoginFailure).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not lock or clear a bucket for an unknown account", async () => {
|
||||
core.getLoginUser.mockResolvedValue(null);
|
||||
expect(await precheckLogin("nonexistent", "pass")).toBe("invalid");
|
||||
expect(core.isLoginLocked).not.toHaveBeenCalled();
|
||||
expect(core.recordLoginFailure).not.toHaveBeenCalled();
|
||||
expect(core.clearLoginLockout).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,11 @@ import {
|
||||
runDummyHashCheck,
|
||||
verifyLoginPassword,
|
||||
} from "@/lib/auth/login-core";
|
||||
import {
|
||||
clearLoginLockout,
|
||||
isLoginLocked,
|
||||
recordLoginFailure,
|
||||
} from "@/lib/auth/login-lockout";
|
||||
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
||||
import { captchaConfig, verifyCaptcha } from "@/lib/services/captcha";
|
||||
|
||||
@@ -15,7 +20,8 @@ export type PrecheckResult =
|
||||
| "invalid"
|
||||
| "twofactor"
|
||||
| "unverified"
|
||||
| "captcha";
|
||||
| "captcha"
|
||||
| "locked";
|
||||
|
||||
/**
|
||||
* Validates username+password WITHOUT creating a session, and reports whether a
|
||||
@@ -38,6 +44,9 @@ export async function precheckLogin(
|
||||
if (!(await verifyCaptcha(captchaToken ?? null, ip))) return "captcha";
|
||||
}
|
||||
|
||||
// A lockout must be checked BEFORE the password is verified: the success
|
||||
// path clears the counter, which would otherwise let an already-locked
|
||||
// account straight back in with the correct credentials.
|
||||
const user = await getLoginUser(u);
|
||||
if (!user) {
|
||||
// Prevent timing-based enumeration: always run a dummy hash check.
|
||||
@@ -45,8 +54,15 @@ export async function precheckLogin(
|
||||
return "invalid";
|
||||
}
|
||||
|
||||
if (await isLoginLocked(user.id)) return "locked";
|
||||
|
||||
const res = await verifyLoginPassword(user, p);
|
||||
if (!res.valid) return "invalid";
|
||||
if (!res.valid) {
|
||||
await recordLoginFailure(user.id);
|
||||
return "invalid";
|
||||
}
|
||||
|
||||
await clearLoginLockout(user.id);
|
||||
|
||||
if (await isEmailUnverified(user)) {
|
||||
return "unverified";
|
||||
|
||||
@@ -41,7 +41,11 @@ const {
|
||||
});
|
||||
|
||||
vi.mock("@/lib/admin/guard", () => ({ requirePermission: vi.fn() }));
|
||||
vi.mock("@/lib/permissions", () => ({ PERMS: { USERS_EDIT: "users.edit" } }));
|
||||
vi.mock("@/lib/permissions", () => ({
|
||||
PERMS: { USERS_EDIT: "users.edit" },
|
||||
// Staff (rank 7) may act on anyone below the hotel's top rank.
|
||||
getHighestRank: vi.fn(() => Promise.resolve(10)),
|
||||
}));
|
||||
vi.mock("@/lib/db", () => ({
|
||||
db: {
|
||||
delete: vi.fn(() => ({ where: deleteWhere })),
|
||||
@@ -109,7 +113,10 @@ beforeEach(() => {
|
||||
onDuplicateKeyUpdate.mockResolvedValue([{ affectedRows: 1 }]);
|
||||
updateWhere.mockResolvedValue([{ affectedRows: 1 }]);
|
||||
selectLimit.mockResolvedValue([]);
|
||||
selectWhereResolved.mockResolvedValue([]);
|
||||
// Rank rows for the per-id rank guard: every target sits below staff rank 7.
|
||||
selectWhereResolved.mockResolvedValue([{ rank: 1 }]);
|
||||
// Max slot of existing badges (consumed by the badge loop, not the guard).
|
||||
selectWhereResolved.mockResolvedValueOnce([{ rank: 1 }]);
|
||||
});
|
||||
|
||||
describe("bulkUnban", () => {
|
||||
|
||||
+101
-32
@@ -1,6 +1,7 @@
|
||||
"use server";
|
||||
|
||||
import { and, eq, inArray, max, sql } from "drizzle-orm";
|
||||
import { isDynamicSuperAdmin } from "@/lib/admin/authorization-policy";
|
||||
import { requirePermission } from "@/lib/admin/guard";
|
||||
import {
|
||||
Ban,
|
||||
@@ -11,18 +12,69 @@ import {
|
||||
UsersCurrency,
|
||||
UsersSettings,
|
||||
} from "@/lib/db";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { getHighestRank, PERMS } from "@/lib/permissions";
|
||||
import type { ActionResult } from "@/lib/safe-action-shared";
|
||||
import { rcon } from "@/lib/services/rcon";
|
||||
import { logStaffActivity } from "@/lib/services/staff-activity";
|
||||
|
||||
/**
|
||||
* Bulk actions are plain server actions whose arguments come from the client,
|
||||
* so every one of them validates the payload and the target ranks first. The
|
||||
* helpers below are the whole "is this allowed" contract.
|
||||
*/
|
||||
const MAX_BULK_USERS = 200;
|
||||
|
||||
function parseUserIds(raw: unknown): number[] {
|
||||
if (!Array.isArray(raw)) return [];
|
||||
const ids = raw
|
||||
.map((v) => (typeof v === "number" ? v : Number(v)))
|
||||
.filter((v) => Number.isInteger(v) && v > 0);
|
||||
return [...new Set(ids)].slice(0, MAX_BULK_USERS);
|
||||
}
|
||||
|
||||
function toPositiveInt(raw: unknown): number | null {
|
||||
const n = typeof raw === "number" ? raw : Number(raw);
|
||||
return Number.isInteger(n) && n > 0 ? n : null;
|
||||
}
|
||||
|
||||
function parseAmount(raw: unknown, max = 1_000_000): number | null {
|
||||
const n = typeof raw === "number" ? raw : Number(raw);
|
||||
return Number.isInteger(n) && n > 0 && n <= max ? n : null;
|
||||
}
|
||||
|
||||
function parseDuration(raw: unknown): number {
|
||||
const n = typeof raw === "number" ? raw : Number(raw);
|
||||
return Number.isInteger(n) && n > 0 ? Math.min(n, 60 * 60 * 24 * 365) : 0;
|
||||
}
|
||||
|
||||
async function guardBulkTargets(
|
||||
staff: { id: number; rank: number },
|
||||
userIds: number[],
|
||||
): Promise<void> {
|
||||
const highestRank = await getHighestRank();
|
||||
const superAdmin = isDynamicSuperAdmin(staff.rank, highestRank);
|
||||
if (superAdmin || userIds.length === 0) return;
|
||||
const rows = await db
|
||||
.select({ rank: User.rank })
|
||||
.from(User)
|
||||
.where(inArray(User.id, userIds));
|
||||
const blocked = rows.filter((r) => r.rank >= staff.rank);
|
||||
if (blocked.length > 0) {
|
||||
throw new Error(
|
||||
"Cannot act on a user at or above your rank — those ids were skipped",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function bulkUnban({
|
||||
userIds,
|
||||
}: {
|
||||
userIds: number[];
|
||||
}): Promise<ActionResult<{ unbanned: number; total: number }>> {
|
||||
const staff = await requirePermission(PERMS.USERS_EDIT);
|
||||
const result = await db.delete(Ban).where(inArray(Ban.userId, userIds));
|
||||
const ids = parseUserIds(userIds);
|
||||
await guardBulkTargets(staff, ids);
|
||||
const result = await db.delete(Ban).where(inArray(Ban.userId, ids));
|
||||
const unbanned = Number(result[0]?.affectedRows ?? 0);
|
||||
await logStaffActivity({
|
||||
staffId: staff.id,
|
||||
@@ -30,10 +82,7 @@ export async function bulkUnban({
|
||||
description: `Unbanned ${unbanned} user(s)`,
|
||||
targetType: "user",
|
||||
});
|
||||
return {
|
||||
ok: true as const,
|
||||
data: { unbanned, total: userIds.length },
|
||||
};
|
||||
return { ok: true as const, data: { unbanned, total: ids.length } };
|
||||
}
|
||||
|
||||
export async function bulkBan({
|
||||
@@ -46,10 +95,14 @@ export async function bulkBan({
|
||||
duration: number;
|
||||
}): Promise<ActionResult<{ banned: number }>> {
|
||||
const staff = await requirePermission(PERMS.USERS_EDIT);
|
||||
const ids = parseUserIds(userIds);
|
||||
const seconds = parseDuration(duration);
|
||||
const reasonText = typeof reason === "string" ? reason.slice(0, 255) : "";
|
||||
await guardBulkTargets(staff, ids);
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
let banned = 0;
|
||||
|
||||
for (const userId of userIds) {
|
||||
for (const userId of ids) {
|
||||
try {
|
||||
await db.insert(Ban).values({
|
||||
userId,
|
||||
@@ -57,8 +110,8 @@ export async function bulkBan({
|
||||
machineId: "",
|
||||
userStaffId: staff.id,
|
||||
timestamp: now,
|
||||
banExpire: duration > 0 ? now + duration : 0,
|
||||
banReason: reason,
|
||||
banExpire: seconds > 0 ? now + seconds : 0,
|
||||
banReason: reasonText,
|
||||
type: "account",
|
||||
});
|
||||
banned++;
|
||||
@@ -92,33 +145,37 @@ export async function bulkGiveCurrency({
|
||||
}>
|
||||
> {
|
||||
const staff = await requirePermission(PERMS.USERS_EDIT);
|
||||
const ids = parseUserIds(userIds);
|
||||
const value = parseAmount(amount);
|
||||
if (!value) throw new Error("Invalid amount");
|
||||
await guardBulkTargets(staff, ids);
|
||||
let given = 0;
|
||||
const failedIds: Array<{ userId: number; reason: string }> = [];
|
||||
|
||||
for (const userId of userIds) {
|
||||
for (const userId of ids) {
|
||||
try {
|
||||
if (type === "credits") {
|
||||
await db
|
||||
.update(User)
|
||||
.set({ credits: sql`${User.credits} + ${amount}` })
|
||||
.set({ credits: sql`${User.credits} + ${value}` })
|
||||
.where(eq(User.id, userId));
|
||||
await rcon.giveCredits(userId, amount);
|
||||
await rcon.giveCredits(userId, value);
|
||||
} else if (type === "pixels") {
|
||||
await db
|
||||
.insert(UsersCurrency)
|
||||
.values({ userId, type: 0, amount })
|
||||
.values({ userId, type: 0, amount: value })
|
||||
.onDuplicateKeyUpdate({
|
||||
set: { amount: sql`${UsersCurrency.amount} + ${amount}` },
|
||||
set: { amount: sql`${UsersCurrency.amount} + ${value}` },
|
||||
});
|
||||
await rcon.giveDuckets(userId, amount);
|
||||
await rcon.giveDuckets(userId, value);
|
||||
} else if (type === "points") {
|
||||
await db
|
||||
.insert(UsersCurrency)
|
||||
.values({ userId, type: 101, amount })
|
||||
.values({ userId, type: 101, amount: value })
|
||||
.onDuplicateKeyUpdate({
|
||||
set: { amount: sql`${UsersCurrency.amount} + ${amount}` },
|
||||
set: { amount: sql`${UsersCurrency.amount} + ${value}` },
|
||||
});
|
||||
await rcon.givePointsGotw(userId, amount);
|
||||
await rcon.givePointsGotw(userId, value);
|
||||
}
|
||||
given++;
|
||||
} catch {
|
||||
@@ -129,7 +186,7 @@ export async function bulkGiveCurrency({
|
||||
await logStaffActivity({
|
||||
staffId: staff.id,
|
||||
action: "bulk_give_currency",
|
||||
description: `Gave ${amount} ${type} to ${given} user(s)`,
|
||||
description: `Gave ${value} ${type} to ${given} user(s)`,
|
||||
targetType: "user",
|
||||
});
|
||||
return {
|
||||
@@ -152,19 +209,21 @@ export async function bulkGiveBadge({
|
||||
}>
|
||||
> {
|
||||
const staff = await requirePermission(PERMS.USERS_EDIT);
|
||||
const ids = parseUserIds(userIds);
|
||||
const code =
|
||||
typeof badgeCode === "string" ? badgeCode.trim().slice(0, 64) : "";
|
||||
if (!code) throw new Error("Invalid badge code");
|
||||
await guardBulkTargets(staff, ids);
|
||||
let given = 0;
|
||||
const failedIds: Array<{ userId: number; reason: string }> = [];
|
||||
|
||||
for (const userId of userIds) {
|
||||
for (const userId of ids) {
|
||||
try {
|
||||
const [existing] = await db
|
||||
.select({ id: UsersBadges.id })
|
||||
.from(UsersBadges)
|
||||
.where(
|
||||
and(
|
||||
eq(UsersBadges.userId, userId),
|
||||
eq(UsersBadges.badgeCode, badgeCode),
|
||||
),
|
||||
and(eq(UsersBadges.userId, userId), eq(UsersBadges.badgeCode, code)),
|
||||
)
|
||||
.limit(1);
|
||||
if (!existing) {
|
||||
@@ -173,8 +232,10 @@ export async function bulkGiveBadge({
|
||||
.from(UsersBadges)
|
||||
.where(eq(UsersBadges.userId, userId));
|
||||
const slotId = (agg?.maxSlot ?? 0) + 1;
|
||||
await db.insert(UsersBadges).values({ userId, slotId, badgeCode });
|
||||
await rcon.giveBadge(userId, badgeCode);
|
||||
await db
|
||||
.insert(UsersBadges)
|
||||
.values({ userId, slotId, badgeCode: code });
|
||||
await rcon.giveBadge(userId, code);
|
||||
}
|
||||
given++;
|
||||
} catch {
|
||||
@@ -211,12 +272,17 @@ export async function bulkAdjustCurrency({
|
||||
}>
|
||||
> {
|
||||
const staff = await requirePermission(PERMS.USERS_EDIT);
|
||||
const ids = parseUserIds(userIds);
|
||||
if (!Number.isFinite(amount) || amount === 0) {
|
||||
return { ok: false as const, error: "Amount must be a non-zero number" };
|
||||
}
|
||||
if (Math.abs(Math.trunc(amount)) > 1_000_000) {
|
||||
return { ok: false as const, error: "Amount is too large" };
|
||||
}
|
||||
await guardBulkTargets(staff, ids);
|
||||
|
||||
if (amount > 0) {
|
||||
const given = await bulkGiveCurrency({ userIds, amount, type });
|
||||
const given = await bulkGiveCurrency({ userIds: ids, amount, type });
|
||||
if (!given.ok) return given;
|
||||
if (!given.data) {
|
||||
return { ok: false as const, error: "Currency adjustment failed" };
|
||||
@@ -235,7 +301,7 @@ export async function bulkAdjustCurrency({
|
||||
let adjusted = 0;
|
||||
const failedIds: Array<{ userId: number; reason: string }> = [];
|
||||
|
||||
for (const userId of userIds) {
|
||||
for (const userId of ids) {
|
||||
try {
|
||||
if (type === "credits") {
|
||||
const [user] = await db
|
||||
@@ -299,8 +365,11 @@ export async function setTradeLock({
|
||||
untilUnix: number;
|
||||
}): Promise<ActionResult<{ userId: number; untilUnix: number }>> {
|
||||
const staff = await requirePermission(PERMS.USERS_EDIT);
|
||||
const until = Math.max(0, Math.trunc(untilUnix));
|
||||
const id = toPositiveInt(userId);
|
||||
if (!id) return { ok: false as const, error: "Invalid user" };
|
||||
const until = Math.max(0, Math.min(Math.trunc(untilUnix), 2_000_000_000));
|
||||
const locked = until > 0;
|
||||
await guardBulkTargets(staff, [id]);
|
||||
|
||||
const [user] = await db
|
||||
.select({
|
||||
@@ -309,7 +378,7 @@ export async function setTradeLock({
|
||||
online: User.online,
|
||||
})
|
||||
.from(User)
|
||||
.where(eq(User.id, userId))
|
||||
.where(eq(User.id, id))
|
||||
.limit(1);
|
||||
if (!user) {
|
||||
return { ok: false as const, error: "User not found" };
|
||||
@@ -331,7 +400,7 @@ export async function setTradeLock({
|
||||
.where(eq(Sanctions.id, existing.id));
|
||||
} else {
|
||||
await tx.insert(Sanctions).values({
|
||||
habboId: userId,
|
||||
habboId: id,
|
||||
tradeLockedUntil: until,
|
||||
reason: locked ? "Trade lock (CMS)" : "",
|
||||
});
|
||||
@@ -369,5 +438,5 @@ export async function setTradeLock({
|
||||
targetId: userId,
|
||||
});
|
||||
|
||||
return { ok: true as const, data: { userId, untilUnix: until } };
|
||||
return { ok: true as const, data: { userId: id, untilUnix: until } };
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
restoreDeletedCatalogItemsCommand,
|
||||
} from "@/features/catalog/server/item-deletes";
|
||||
import {
|
||||
bulkUpdateOffersCommand,
|
||||
createOfferCommand,
|
||||
moveBcOffersCommand,
|
||||
moveOffersCommand,
|
||||
@@ -399,6 +400,91 @@ export async function updateCatalogItem({
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* "Save All" for the inline table editor. One transaction, one RCON refresh,
|
||||
* one audit row — instead of a full per-item round-trip that took tens of
|
||||
* seconds on pages with hundreds of edited rows. A failing row rolls back the
|
||||
* whole batch, so the editor never ends up half-saved.
|
||||
*/
|
||||
export async function updateCatalogItemsBulk({
|
||||
updates,
|
||||
catalog = "normal",
|
||||
}: {
|
||||
updates: Array<{ id: number; catalogFields: Record<string, unknown> }>;
|
||||
catalog?: "normal" | "bc";
|
||||
}) {
|
||||
const staff = await requirePermission(PERMS.CATALOG_EDIT);
|
||||
if (!Array.isArray(updates) || updates.length === 0) {
|
||||
return { ok: false as const, error: "No items to update" };
|
||||
}
|
||||
if (updates.length > 500) {
|
||||
return { ok: false as const, error: "Max 500 items per bulk update" };
|
||||
}
|
||||
// BC rows go through their own command per item (no price columns, and the
|
||||
// BC command keeps its narrower field set); the loop still saves the
|
||||
// per-item RCON refreshes inside one export lock below.
|
||||
if (catalog === "bc") {
|
||||
const { updateBcItem } = await import("@/actions/catalog-bc");
|
||||
return await withCatalogExport(async () => {
|
||||
let savedCount = 0;
|
||||
let lastError = "";
|
||||
for (const update of updates) {
|
||||
const result = await updateBcItem({
|
||||
id: update.id,
|
||||
...(update.catalogFields as {
|
||||
itemIds?: string;
|
||||
catalogName?: string;
|
||||
orderNumber?: number;
|
||||
extradata?: string;
|
||||
}),
|
||||
});
|
||||
if (result.ok) savedCount++;
|
||||
else lastError = result.error ?? "Unable to update item";
|
||||
}
|
||||
if (savedCount > 0) {
|
||||
await logStaffActivity({
|
||||
staffId: staff.id,
|
||||
action: "bc_items_bulk_update",
|
||||
description: `Updated ${savedCount} BC catalog item(s)`,
|
||||
targetType: "catalog_item_bc",
|
||||
});
|
||||
}
|
||||
if (savedCount === 0 && lastError) {
|
||||
return { ok: false as const, error: lastError };
|
||||
}
|
||||
return {
|
||||
ok: true as const,
|
||||
data: { savedCount, failedCount: updates.length - savedCount },
|
||||
};
|
||||
});
|
||||
}
|
||||
return await withCatalogExport(async () => {
|
||||
try {
|
||||
await bulkUpdateOffersCommand(updates, staff.id);
|
||||
} catch (error) {
|
||||
return {
|
||||
ok: false as const,
|
||||
error:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Unable to update catalog items",
|
||||
};
|
||||
}
|
||||
await sendCatalogUpdate();
|
||||
await logStaffActivity({
|
||||
staffId: staff.id,
|
||||
action: "catalog_items_bulk_update",
|
||||
description: `Updated ${updates.length} catalog item(s)`,
|
||||
targetType: "catalog_item",
|
||||
});
|
||||
revalidatePath("/admin/catalog");
|
||||
return {
|
||||
ok: true as const,
|
||||
data: { savedCount: updates.length, failedCount: 0 },
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export async function translateCatalogItems(input: {
|
||||
/** `id` is items_base.id (not catalog_items.id) */
|
||||
items: Array<{ id: number; publicName: string; description?: string }>;
|
||||
|
||||
@@ -52,6 +52,13 @@ const mockSetMotto = vi.hoisted(() => vi.fn());
|
||||
const mockSetRank = vi.hoisted(() => vi.fn());
|
||||
const mockExecuteCommand = vi.hoisted(() => vi.fn());
|
||||
const mockSendGift = vi.hoisted(() => vi.fn());
|
||||
// The audit service is exercised separately; here it only has to be harmless.
|
||||
// Mocked explicitly because the fake db has no `insert`, which used to leak an
|
||||
// unhandled rejection out of the fire-and-forget audit call.
|
||||
vi.mock("@/lib/services/audit", () => ({
|
||||
logAudit: vi.fn(async () => undefined),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/services/rcon", () => ({
|
||||
rcon: {
|
||||
send: mockSend,
|
||||
@@ -455,3 +462,25 @@ describe("access control", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("auditing", () => {
|
||||
it("logs an entry for a currency grant", async () => {
|
||||
const { logAudit } = await import("@/lib/services/audit");
|
||||
await giveCredits({ userId: 1, credits: 100 });
|
||||
expect(logAudit).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ action: expect.any(String) }),
|
||||
);
|
||||
});
|
||||
|
||||
it("completes the command even when auditing rejects", async () => {
|
||||
const { logAudit } = await import("@/lib/services/audit");
|
||||
vi.mocked(logAudit).mockRejectedValueOnce(new Error("audit table missing"));
|
||||
await expect(giveCredits({ userId: 1, credits: 100 })).resolves.toEqual({
|
||||
ok: true,
|
||||
data: {},
|
||||
});
|
||||
// Let the fire-and-forget promise settle; an unhandled rejection here is
|
||||
// exactly the failure this guards against.
|
||||
await new Promise((r) => setTimeout(r, 0));
|
||||
});
|
||||
});
|
||||
Loaded 100 of 474 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user