# Sitespeed.io Worker — HTTP API wrapping the sitespeed CLI.
#
# Built on top of the official sitespeed.io image so the CLI + headless
# Chrome + lighthouse stack are already present. We add Node 22 (the
# official sitespeed images bundle an older node used internally) and
# the worker source code on top.
#
# Build:    docker compose build sitespeed-worker
# Run:      docker compose up -d sitespeed-worker
# Shell in: docker compose exec sitespeed-worker bash

# syntax=docker/dockerfile:1.7

############################
# Stage 1 — deps (pnpm)
############################
FROM node:22-bookworm-slim AS deps

WORKDIR /app

# Enable corepack so we get a pinned pnpm without polluting node_modules.
RUN corepack enable && corepack prepare pnpm@10.33.0 --activate

COPY package.json pnpm-lock.yaml ./

# Install ALL deps (incl. dev so we have tsx + typescript available
# at runtime — server boots through tsx, no compile step).
RUN pnpm install --frozen-lockfile


############################
# Stage 2 — runtime (sitespeed + Node 22 + worker)
############################
FROM sitespeedio/sitespeed.io:41.1.0-plus1 AS runtime

USER root

# Install Node 22 from NodeSource on top of the sitespeed image.
RUN apt-get update \
    && apt-get install -y --no-install-recommends curl ca-certificates gnupg tini \
    && curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
    && apt-get install -y --no-install-recommends nodejs \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy hydrated node_modules + manifests from the deps stage.
COPY --from=deps /app/node_modules ./node_modules
COPY package.json pnpm-lock.yaml ./

# App source.
COPY tsconfig.json ./
COPY src ./src

# Results dir — mounted as a named volume in docker-compose.
RUN mkdir -p /data/results

ENV NODE_ENV=production \
    PORT=3001 \
    RESULTS_DIR=/data/results \
    NODE_OPTIONS=--enable-source-maps

EXPOSE 3001

# Override the sitespeed image's default entrypoint (which would launch
# the CLI). Tini reaps zombie Chrome / sitespeed children.
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["node", "--import", "tsx/esm", "src/server.ts"]
