Files
supabase/apps/studio/scripts/serve.js
Alaister Young ad181489b1 feat(studio): adopt @sentry/tanstackstart-react server instrumentation on the TanStack build (#47724)
Stacked on #47666 (base `alaister/tanstack-sentry-init`; retarget to
`master` when that merges). **Supersedes #47721** (the manual
`@sentry/node` wrapper). Client stays on #47666's `@sentry/react` setup.

Adopts the official `@sentry/tanstackstart-react` SDK **on the server
only**, after a spike (#47723) evaluating the full unified client+server
SDK. The spike found the SDK's **browser**
`tanstackRouterBrowserTracingIntegration` is a broken no-op stub at
10.59.0/10.64.0 — so the client stays on `@sentry/react` (whose
equivalent integration is a real, working implementation, already
shipped in #47666). The **server** exports, however, are a clear upgrade
and slot in cleanly.

### What this adds (server-side, TanStack build only)
- **`instrument.server.mjs`** — `Sentry.init` from
`@sentry/tanstackstart-react`, mirroring `sentry.server.config.ts` +
`release: VERCEL_GIT_COMMIT_SHA`.
- **`start.ts`** — `sentryGlobalRequestMiddleware` +
`sentryGlobalFunctionMiddleware` at the front of the existing
`createStart(...)` middleware. **This is the win**: it captures request-
and server-function errors *including the ones swallowed into 500s* —
the exact class the manual wrapper (and the Next server SDK) miss.
- **`api/server.js` / `scripts/serve.js`** — gated
(`STUDIO_FRAMEWORK==='tanstack'`) instrument init +
`wrapFetchWithSentry` on the handler.
- **`vite.config.ts`** — `sentryTanstackStart({ …,
autoInstrumentMiddleware: false })` as the last plugin: source-map
upload + release injection (skips gracefully without an auth token).
Middleware is wired explicitly rather than via the plugin's
string-rewrite.

### Guarantees
- **Client untouched** — the `@sentry/nextjs`→`@sentry/react` alias and
#47666's client init are unchanged.
- **Next untouched** — `instrumentation.ts` / `sentry.server.config.ts`
etc. stay as-is; all new code is TanStack-gated.
- **No server SDK in the client bundle** — verified after build: no
`@sentry/node` / server middleware / `wrapFetchWithSentry` in
`dist/client/assets` (`start.ts`'s server import is tree-shaken out).

### Verified
TanStack build exit 0 (past `assertNoChunkCycles`), post-build server
boot served `/api/get-utc-time → 200`, `tsc --noEmit` clean,
prettier/eslint clean. Node smoke: no-DSN init is a clean no-op; wrapped
handler returns 200.

### To test (deploy with a server DSN)
Throw a server error from an `/api/*` route (or a `/_serverFn/*`) —
including one that gets turned into a 500 without rethrowing — and
confirm a server event in Sentry with `release` = the deploy SHA.
Compared to #47721, the swallowed-500 case should now be captured via
the middleware.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added Sentry integration for the Studio app’s TanStack Start runtime,
including request and server-function instrumentation.
* Wrapped server request handling to capture errors reliably, with
tracing enabled.
* Updated build tooling to conditionally upload source maps when
credentials are present.

* **Bug Fixes**
* Improved resilience by safely falling back to a no-op Sentry setup if
instrumentation cannot be loaded.
* Ensured existing request protection remains enabled while adding
observability middleware.

* **Chores / Config**
* Added `SKIP_ASSET_UPLOAD` to the build environment list to control
cache/build behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Alaister Young <[email protected]>
Co-authored-by: Joshen Lim <[email protected]>
2026-07-10 16:52:07 +08:00

215 lines
8.1 KiB
JavaScript

#!/usr/bin/env node
// Standalone Node HTTP server that hosts the production studio build.
//
// We export the fetch-handler shape from `dist/server/server.js` because
// Vercel consumes it directly (see `apps/studio/api/server.js`). For
// self-hosted / e2e, we need an HTTP listener of our own — this is that
// listener.
//
// Responsibilities:
// - Load env files in vite preview's order so non-NEXT_PUBLIC_* values
// (POSTGRES_PASSWORD, PG_META_CRYPTO_KEY, etc.) are in process.env
// at request time. NEXT_PUBLIC_* are already inlined into the bundle
// at build time and don't need to be re-loaded.
// - Serve static client assets from `dist/client/` directly with the
// right MIME types and cache headers.
// - Forward everything else to the TanStack Start handler exported
// from `dist/server/server.js`.
import { createReadStream } from 'node:fs'
import { stat } from 'node:fs/promises'
import { createServer } from 'node:http'
import path from 'node:path'
import { Readable } from 'node:stream'
import { fileURLToPath } from 'node:url'
import { readEnvFiles } from './lib/env.js'
const studioRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
const clientDir = path.join(studioRoot, 'dist/client')
const mode = process.env.MODE || 'production'
const envFiles = ['.env', '.env.local', `.env.${mode}`, `.env.${mode}.local`]
const parsed = readEnvFiles(studioRoot, envFiles)
// Don't clobber values the shell already provides — match `vite preview`.
for (const [k, v] of Object.entries(parsed)) {
if (process.env[k] !== undefined) continue
process.env[k] = v.replace(
/\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?/g,
(_, name) => process.env[name] ?? parsed[name] ?? ''
)
}
// Initialize server-side Sentry now that .env values are in process.env (the
// instrument module reads process.env.NEXT_PUBLIC_SENTRY_DSN at call time).
// Imported dynamically here rather than via a `--import` flag so it runs after
// the env-loading block above. Non-fatal if the SDK isn't present.
try {
await import(path.join(studioRoot, 'instrument.server.mjs'))
} catch (err) {
console.warn('[serve] Sentry server init skipped:', err?.message ?? err)
}
const { wrapFetchWithSentry } = await import('@sentry/tanstackstart-react').catch(() => ({
wrapFetchWithSentry: (fetchHandler) => fetchHandler,
}))
const { default: rawHandler } = await import(path.join(studioRoot, 'dist/server/server.js'))
// Wrap so request-scoped errors (incl. ones swallowed into a 500) are captured.
const handler = {
...rawHandler,
fetch: wrapFetchWithSentry(rawHandler.fetch.bind(rawHandler)),
}
const mimeByExt = new Map([
['.js', 'application/javascript; charset=utf-8'],
['.mjs', 'application/javascript; charset=utf-8'],
['.css', 'text/css; charset=utf-8'],
['.html', 'text/html; charset=utf-8'],
['.json', 'application/json; charset=utf-8'],
['.map', 'application/json; charset=utf-8'],
['.png', 'image/png'],
['.jpg', 'image/jpeg'],
['.jpeg', 'image/jpeg'],
['.gif', 'image/gif'],
['.svg', 'image/svg+xml'],
['.ico', 'image/x-icon'],
['.woff', 'font/woff'],
['.woff2', 'font/woff2'],
['.txt', 'text/plain; charset=utf-8'],
['.webmanifest', 'application/manifest+json'],
])
// Vite emits hashed filenames (e.g. `index-DB4J79t9.js`) for everything
// it bundles. Those are content-addressed so we serve them immutable.
const HASHED_RE = /-[A-Za-z0-9_-]{6,}\.[a-z0-9]+$/
async function serveStatic(req, res) {
let pathname
try {
pathname = new URL(req.url, 'http://localhost').pathname
} catch {
return false
}
if (pathname === '/' || pathname.endsWith('/')) return false
if (pathname.includes('..') || pathname.includes('\\')) return false
const filePath = path.join(clientDir, pathname)
if (!filePath.startsWith(clientDir + path.sep)) return false
let st
try {
st = await stat(filePath)
} catch {
return false
}
if (!st.isFile()) return false
res.statusCode = 200
res.setHeader(
'content-type',
mimeByExt.get(path.extname(filePath).toLowerCase()) ?? 'application/octet-stream'
)
res.setHeader('content-length', String(st.size))
res.setHeader(
'cache-control',
HASHED_RE.test(pathname) ? 'public, max-age=31536000, immutable' : 'no-cache'
)
await new Promise((resolve, reject) => {
const stream = createReadStream(filePath)
stream.on('error', reject)
stream.on('end', resolve)
stream.pipe(res)
})
return true
}
function toWebRequest(req) {
const protocol = req.socket.encrypted ? 'https' : 'http'
const url = `${protocol}://${req.headers.host ?? 'localhost'}${req.url}`
const headers = new Headers()
for (const [k, v] of Object.entries(req.headers)) {
if (k.startsWith(':')) continue
if (Array.isArray(v)) for (const vv of v) headers.append(k, vv)
else if (v !== undefined) headers.set(k, v)
}
const init = { method: req.method, headers }
// Only attach a body for methods that can carry one AND that actually
// have body bytes coming. Wrapping `req` in `Readable.toWeb(req)` for
// requests where Node has nothing to deliver leaves undici's
// `extractBody` looking at an already-consumed stream and throwing
// `TypeError: Response body object should not be disturbed or locked`
// at the `new Request(...)` call below.
const contentLength = Number(req.headers['content-length'] ?? '0')
const hasBody =
req.method !== 'GET' &&
req.method !== 'HEAD' &&
(contentLength > 0 || req.headers['transfer-encoding'] === 'chunked')
if (hasBody) {
init.body = Readable.toWeb(req)
init.duplex = 'half'
}
return new Request(url, init)
}
async function pipeWebResponse(response, res) {
res.statusCode = response.status
// The Headers iterator collapses duplicate keys, and for `set-cookie` it joins
// every cookie into one comma-separated value — which corrupts auth/session
// cookies. Pull the cookies out separately via getSetCookie() and set them as
// an array so each one becomes its own header.
const setCookies =
typeof response.headers.getSetCookie === 'function' ? response.headers.getSetCookie() : []
for (const [k, v] of response.headers) {
if (k.toLowerCase() === 'set-cookie') continue
res.setHeader(k, v)
}
if (setCookies.length > 0) res.setHeader('set-cookie', setCookies)
if (!response.body) {
res.end()
return
}
// Pipe via Readable.fromWeb so the underlying stream gets proper backpressure
// and gets released cleanly. `for await (chunk of response.body)` works in
// simple cases but can leave the body in a "disturbed / locked" state when
// the handler internally peeks at it — surfacing as
// `TypeError: Response body object should not be disturbed or locked` on a
// subsequent request.
await new Promise((resolve, reject) => {
const readable = Readable.fromWeb(response.body)
readable.on('error', reject)
res.on('error', reject)
res.on('close', resolve)
res.on('finish', resolve)
readable.pipe(res)
})
}
// Security headers for the self-hosted server. Mirrors the non-platform branch
// of next.config.ts `headers()` (self-hosted is always IS_PLATFORM=false, so the
// CSP is just `frame-ancestors 'none'` and there's no HSTS). The platform CSP is
// applied at the edge via vercel.ts instead; see security-headers.ts. Set before
// any response is written so both the static and handler paths inherit them.
const SECURITY_HEADERS = [
['X-Frame-Options', 'DENY'],
['X-Content-Type-Options', 'nosniff'],
['Content-Security-Policy', "frame-ancestors 'none';"],
['Referrer-Policy', 'strict-origin-when-cross-origin'],
]
const port = Number(process.env.PORT || 8082)
createServer(async (req, res) => {
try {
for (const [key, value] of SECURITY_HEADERS) res.setHeader(key, value)
if (await serveStatic(req, res)) return
const response = await handler.fetch(toWebRequest(req))
await pipeWebResponse(response, res)
} catch (err) {
console.error('[serve] request failed:', err)
if (!res.headersSent) {
res.statusCode = 500
res.setHeader('content-type', 'text/plain; charset=utf-8')
}
res.end('Internal Server Error')
}
}).listen(port, () => {
console.log(`Studio listening on http://localhost:${port} (mode=${mode})`)
})