mirror of
https://github.com/stack-auth/stack.git
synced 2026-07-20 21:29:36 +08:00
> **Draft / WIP — do not merge.** Backend Next.js → ElysiaJS transport
migration, in progress.
## Summary
Migrates the backend (`apps/backend`) HTTP transport from **Next.js (App
Router)** to **ElysiaJS** (Node adapter, `@elysiajs/node`), with the
explicit goal of keeping every API route **byte-for-byte backwards
compatible** — same URLs, methods, status codes, headers, and bodies.
Elysia becomes purely the transport layer; the existing
`createSmartRouteHandler` abstraction and all ~221 file-based route
handlers are reused unchanged.
## Approach
- **One wildcard dispatcher** reuses the existing `smart-router`
matchers + generated `routes.json`/`api-versions.json` rather than
re-registering 221 routes on Elysia's router.
- **`proxy.tsx` (Next 16 middleware) ported** to an Elysia request
pipeline: CORS, dev rate-limit, `x-hexclave-*`→`x-stack-*` header
aliasing, OPTIONS preflight, and the **API version rewrite** (`/api/v1`,
`/api/v2betaN` → `/api/latest` / `/api/migrations/*`).
- **Two-URL dispatch**: the version-rewritten path locates the handler;
the original client URL is preserved on `req.url`/`nextUrl`
(load-bearing for OIDC/neon routes that assert `/api/v1/...`).
- **`next/*` compatibility shims** (`next/headers`, `next/navigation`,
`next/server`) under `src/lib/next-compat/`, wired via aliases in
tsconfig / vitest / tsdown so route files stay untouched.
- **Observability** moved off `@sentry/nextjs` → `@sentry/node` + manual
OpenTelemetry NodeSDK preload (`src/instrument.ts`); explicit
`/monitoring` Sentry tunnel.
- **Build/deploy**: `next build/start` → tsdown bundle
(`tsdown.config.ts`) for container; Vercel default-export entry;
Dockerfile CMD → `dist/server.mjs`; `next.config.mjs` removed (security
headers etc. reimplemented).
## Status — WIP
**Green so far:** backend `typecheck`, backend `lint`, tsdown bundle,
`GET /health`, `GET /api/v1`, e2e `migration-tests` (13/13),
`analytics-query` (69/69), unit suite (~1006 tests).
**Remaining before ready for review:**
- [ ] Full backend e2e snapshot suite green (snapshots = byte-for-byte
oracle; never `-u`'d to mask drift)
- [ ] `config.test.ts` (`custom_oidc`) snapshot root-cause
- [ ] M4: source-map upload + remove remaining `@sentry/nextjs` browser
leftovers
- [ ] M5: Docker + Vercel artifact verification
- [ ] M6: confirm React-UI → slim-handler cleanup
## Notes
- Backwards-compat gate: the existing e2e snapshot files (recorded
against the Next.js backend) must pass unchanged.
- Built primarily by Codex (`gpt-5.5`); branch intentionally separate
from `dev`.
<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Migrates the backend transport from Next.js to ElysiaJS with
byte‑for‑byte compatible APIs, bundled with `tsdown` for Node, Vercel,
and Docker. Reverts out‑of‑scope payments/metrics/custom OIDC changes to
keep this PR focused.
- **Refactors**
- Transport: Next.js → ElysiaJS (`elysia`, `@elysiajs/node`) via
wildcard dispatcher + generated route registry; preserve original URL;
decode params; 400 on malformed params.
- Next shims: `next/headers`, `next/navigation`, `next/server` with
request context and cookie serialization; don’t percent‑decode;
`cookies().delete()` clears pending Set‑Cookie; add `fetch` `next`
options typing.
- Observability: move to `@sentry/node`/`@sentry/browser`; preload
`@opentelemetry/sdk-node`; guard duplicate OTel registration; add
`/monitoring` tunnel; sanitize Sentry release names; replace Next.js
instrumentation with `src/instrument.ts`.
- Build/deploy: bundle `dist/server.mjs` and `dist/vercel.mjs` with
`tsdown`; Vercel function at `api/index.ts` re‑exports the handler
(`runtime: nodejs`, `maxDuration: 60`, `framework: null` rewrite);
Docker runs the Node bundle.
- Runtime: reimplement security headers; dev rate limiter uses high‑res
timers; graceful SIGTERM.
- Env/dev: expand nested env refs; load `.env.development` in dev;
TS/Vitest alias `next/*` to shims.
- Deps/tooling: re‑add `@sinclair/typebox`; regenerate `pnpm-lock.yaml`
with pnpm 11.5.0 and pin `exact-mirror@1.1.1`.
- **Bug Fixes**
- Dispatcher: catch `NextNotFoundError` and return 404.
- Emails: deterministic localhost/loopback SMTP fallback (prefer IPv6)
and consistent HTML for snapshot parity.
- E2E parity: restore email‑conflict error; poll all‑users outbox before
asserting; extend refund/transactions timeouts; fix team invitation
revoke setup.
- Build/Docker: prevent `dist` wipes by setting `clean:false` in
db‑migrations `tsdown` config; reorder COPY so `dist/server.mjs` is
present; backend entrypoint uses `dist/server.mjs`.
- CI/tests: Vitest `minWorkers: 1`; fallback e2e starts the Elysia
bundle via `pnpm run start` with `PORT`.
- Shared: normalize `esbuild-wasm` default export under Node to avoid
runtime mismatches.
<sup>Written for commit d3c9b0ff22.
Summary will update on new commits.</sup>
<a
href="https://cubic.dev/pr/hexclave/hexclave/pull/1630?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
## Release Notes
* **Bug Fixes**
* Improved email delivery by adding localhost/loopback-aware SMTP retry
behavior and more consistent delivered email HTML.
* Enhanced error reporting during external database synchronization.
* Refined OAuth provider type validation to better handle missing or
custom provider configurations.
* **Improvements**
* Updated backend runtime and routing/request handling for more
consistent behavior, including updated Vercel/Docker startup.
* Improved payment “dual write” reliability by scheduling Bulldozer
projection updates with per-tenant ordering.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: mantra <mantra@stack-auth.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Bilal Godil <bg2002@gmail.com>
183 lines
7.0 KiB
YAML
183 lines
7.0 KiB
YAML
# TODO: keep in sync with e2e-tests.yaml — this is a near-copy with the backend
|
|
# started on the fallback port (8110) only, so the SDK exercises fallback logic.
|
|
name: Runs E2E Fallback Tests
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
- dev
|
|
pull_request:
|
|
|
|
concurrency:
|
|
group: ${{ github.workflow }}-${{ github.ref }}
|
|
cancel-in-progress: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/dev' }}
|
|
|
|
jobs:
|
|
build:
|
|
name: E2E Fallback Tests (Node ${{ matrix.node-version }})
|
|
runs-on: ubicloud-standard-8
|
|
env:
|
|
NODE_ENV: test
|
|
HEXCLAVE_ENABLE_HARDCODED_PASSKEY_CHALLENGE_FOR_TESTING: yes
|
|
HEXCLAVE_DATABASE_CONNECTION_STRING: "postgres://postgres:PASSWORD-PLACEHOLDER--uqfEC1hmmv@localhost:8128/stackframe"
|
|
HEXCLAVE_EXTERNAL_DB_SYNC_MAX_DURATION_MS: "20000"
|
|
HEXCLAVE_EXTERNAL_DB_SYNC_DIRECT: "false"
|
|
# SDK reads this as the primary URL, discovers hardcoded fallback to port 8110
|
|
NEXT_PUBLIC_HEXCLAVE_API_URL: "http://localhost:8102"
|
|
# Tells js-helpers to omit explicit baseUrl so the SDK exercises fallback logic
|
|
HEXCLAVE_TEST_SDK_FALLBACK: "true"
|
|
|
|
strategy:
|
|
matrix:
|
|
node-version: [22.x]
|
|
|
|
steps:
|
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
|
|
|
|
- name: Setup Node.js ${{ matrix.node-version }}
|
|
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
|
|
with:
|
|
node-version: ${{ matrix.node-version }}
|
|
|
|
- name: Setup pnpm
|
|
uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4
|
|
|
|
- name: Start Docker Compose in background
|
|
uses: JarvusInnovations/background-action@2428e7b970a846423095c79d43f759abf979a635 # v1.0.7
|
|
with:
|
|
run: docker compose -f docker/dependencies/docker.compose.yaml up --pull always -d &
|
|
wait-on: /dev/null
|
|
tail: true
|
|
wait-for: 3s
|
|
log-output-if: true
|
|
|
|
- name: Install dependencies
|
|
run: pnpm install --frozen-lockfile
|
|
|
|
- name: Create .env.test.local files
|
|
run: |
|
|
cp apps/backend/.env.development apps/backend/.env.test.local
|
|
cp apps/dashboard/.env.development apps/dashboard/.env.test.local
|
|
cp apps/e2e/.env.development apps/e2e/.env.test.local
|
|
cp docs/.env.development docs/.env.test.local
|
|
cp examples/cjs-test/.env.development examples/cjs-test/.env.test.local
|
|
cp examples/demo/.env.development examples/demo/.env.test.local
|
|
cp examples/docs-examples/.env.development examples/docs-examples/.env.test.local
|
|
cp examples/e-commerce/.env.development examples/e-commerce/.env.test.local
|
|
cp examples/middleware/.env.development examples/middleware/.env.test.local
|
|
cp examples/supabase/.env.development examples/supabase/.env.test.local
|
|
cp examples/convex/.env.development examples/convex/.env.test.local
|
|
cp apps/internal-tool/.env.development apps/internal-tool/.env.test.local
|
|
|
|
- name: Build
|
|
run: pnpm build
|
|
|
|
- name: Wait on Postgres
|
|
run: pnpm run wait-until-postgres-is-ready:pg_isready
|
|
|
|
- name: Wait on Inbucket
|
|
run: pnpx wait-on tcp:localhost:8129
|
|
|
|
- name: Wait on Svix
|
|
run: pnpx wait-on tcp:localhost:8113
|
|
|
|
- name: Wait on QStash
|
|
run: pnpx wait-on tcp:localhost:8125
|
|
|
|
- name: Wait on ClickHouse
|
|
run: pnpx wait-on http://localhost:8136/ping
|
|
|
|
- name: Initialize database
|
|
run: pnpm run db:init
|
|
|
|
- name: Start bulldozer-js in background
|
|
uses: JarvusInnovations/background-action@2428e7b970a846423095c79d43f759abf979a635 # v1.0.7
|
|
env:
|
|
HEXCLAVE_BULLDOZER_JS_USE_TMP_LMDB: "1"
|
|
with:
|
|
run: pnpm run start:bulldozer &
|
|
wait-on: |
|
|
tcp:localhost:8146
|
|
tail: true
|
|
wait-for: 30s
|
|
log-output-if: true
|
|
|
|
# db:init only seeds Postgres now; mirror the seeded payment rows into the
|
|
# fresh bulldozer-js store so the backend's payment reads aren't empty.
|
|
- name: Backfill Bulldozer from Postgres
|
|
run: pnpm run db:backfill-bulldozer-from-prisma
|
|
|
|
# Start backend ONLY on fallback port 8110 — primary port 8102 is intentionally left down
|
|
# so the SDK exercises its fallback logic for every request.
|
|
- name: Start stack-backend on fallback port (8110)
|
|
uses: JarvusInnovations/background-action@2428e7b970a846423095c79d43f759abf979a635 # v1.0.7
|
|
with:
|
|
run: PORT=8110 pnpm -C apps/backend run with-env:test pnpm run start &
|
|
wait-on: |
|
|
http://localhost:8110
|
|
tail: true
|
|
wait-for: 30s
|
|
log-output-if: true
|
|
|
|
- name: Start stack-dashboard in background
|
|
uses: JarvusInnovations/background-action@2428e7b970a846423095c79d43f759abf979a635 # v1.0.7
|
|
with:
|
|
run: pnpm run start:dashboard --log-order=stream &
|
|
wait-on: |
|
|
http://localhost:8101
|
|
tail: true
|
|
wait-for: 30s
|
|
log-output-if: true
|
|
|
|
- name: Start mock-oauth-server in background
|
|
uses: JarvusInnovations/background-action@2428e7b970a846423095c79d43f759abf979a635 # v1.0.7
|
|
with:
|
|
run: pnpm run start:mock-oauth-server --log-order=stream &
|
|
wait-on: |
|
|
http://localhost:8110
|
|
tail: true
|
|
wait-for: 30s
|
|
log-output-if: true
|
|
|
|
- name: Start run-email-queue in background
|
|
uses: JarvusInnovations/background-action@2428e7b970a846423095c79d43f759abf979a635 # v1.0.7
|
|
with:
|
|
run: pnpm -C apps/backend run run-email-queue --log-order=stream &
|
|
wait-on: |
|
|
http://localhost:8110
|
|
tail: true
|
|
wait-for: 30s
|
|
log-output-if: true
|
|
|
|
- name: Start run-cron-jobs in background
|
|
uses: JarvusInnovations/background-action@2428e7b970a846423095c79d43f759abf979a635 # v1.0.7
|
|
with:
|
|
run: pnpm -C apps/backend run run-cron-jobs:test --log-order=stream &
|
|
wait-on: |
|
|
http://localhost:8110
|
|
tail: true
|
|
wait-for: 30s
|
|
log-output-if: true
|
|
|
|
- name: Wait 10 seconds
|
|
run: sleep 10
|
|
|
|
- name: Verify primary port 8102 is NOT running
|
|
run: |
|
|
if curl -s -o /dev/null -w "%{http_code}" http://localhost:8102/health 2>/dev/null | grep -q "200"; then
|
|
echo "ERROR: Primary backend on port 8102 should NOT be running for fallback tests"
|
|
exit 1
|
|
fi
|
|
echo "Confirmed: primary port 8102 is down, fallback tests will exercise SDK fallback logic"
|
|
|
|
# Only run JS SDK tests — these exercise the SDK's fallback logic.
|
|
# Backend API tests use direct HTTP calls that don't go through fallback.
|
|
# Exclude cross-domain-auth which hardcodes the primary URL.
|
|
- name: Run SDK fallback tests
|
|
run: pnpm -w run pre && cd apps/e2e && npx vitest run tests/js/ --exclude '**/{cross-domain-auth,oauth,email-template-existing-project}*'
|
|
|
|
- name: Print Docker Compose logs
|
|
if: always()
|
|
run: docker compose -f docker/dependencies/docker.compose.yaml logs
|