|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Default data-directory + serverless-platform detection. |
| 5 | + * |
| 6 | + * Single source of truth for the on-disk location of the control-plane |
| 7 | + * SQLite file (`control.db`), per-project SQLite files, and InMemoryDriver |
| 8 | + * persistence JSON files in **non-serverless** deployments. |
| 9 | + * |
| 10 | + * On serverless platforms with a read-only application bundle (Vercel, |
| 11 | + * AWS Lambda, Netlify Functions, Cloudflare Workers Node compat) the |
| 12 | + * file-backed default is unsupported — `/var/task` is read-only and |
| 13 | + * `/tmp` is per-instance, ephemeral, and not shared between concurrent |
| 14 | + * cold starts. Persisting business data there silently corrupts |
| 15 | + * deployments. The recommended (and only sensible) default for these |
| 16 | + * platforms is **Turso / libSQL** — set `TURSO_DATABASE_URL` (or |
| 17 | + * `OS_CONTROL_DATABASE_URL=libsql://…`) and the cloud-stack driver |
| 18 | + * factory will pick it up automatically. |
| 19 | + * |
| 20 | + * Resolution order for {@link resolveDefaultDataDir}: |
| 21 | + * |
| 22 | + * 1. `OS_DATA_DIR` environment variable (explicit override — wins |
| 23 | + * always, even on serverless; intended for self-managed mounts |
| 24 | + * such as a network volume or EFS share). |
| 25 | + * 2. `<cwd>/.objectstack/data` on a writable filesystem (the default |
| 26 | + * for `objectstack dev`, `objectstack serve`, Docker, bare metal, …). |
| 27 | + * 3. **THROWS** on a detected serverless read-only filesystem. The |
| 28 | + * error message tells the user exactly which env var to set. |
| 29 | + * |
| 30 | + * Centralising this logic prevents both |
| 31 | + * (a) the "ENOENT: mkdir '/var/task/.objectstack'" cold-start crash, and |
| 32 | + * (b) the worse failure mode where an ephemeral `/tmp` SQLite "works" |
| 33 | + * for a single cold start and silently loses data on the next one. |
| 34 | + */ |
| 35 | + |
| 36 | +import { resolve as resolvePath } from 'node:path'; |
| 37 | + |
| 38 | +/** |
| 39 | + * Returns `true` when the current process is running on a serverless |
| 40 | + * platform whose application bundle is a read-only filesystem and whose |
| 41 | + * `/tmp` is per-instance / ephemeral. The set of detected platforms |
| 42 | + * intentionally matches the ones where ObjectStack is regularly deployed |
| 43 | + * today; new platforms can be added via the `OS_READONLY_FS=1` escape |
| 44 | + * hatch. |
| 45 | + */ |
| 46 | +export function isServerlessReadOnlyFs(env: NodeJS.ProcessEnv = process.env): boolean { |
| 47 | + if (env.OS_READONLY_FS && ['1', 'true', 'yes', 'on'].includes(env.OS_READONLY_FS.trim().toLowerCase())) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + // Vercel sets VERCEL=1 in all build & runtime environments. |
| 51 | + if (env.VERCEL === '1') return true; |
| 52 | + // AWS Lambda & Lambda@Edge. |
| 53 | + if (env.AWS_LAMBDA_FUNCTION_NAME) return true; |
| 54 | + // Netlify Functions. |
| 55 | + if (env.NETLIFY === 'true' || env.NETLIFY_DEV) return true; |
| 56 | + return false; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Build the standard "configure a persistent database" error message |
| 61 | + * shown when a file-backed default is requested on serverless. |
| 62 | + * @internal |
| 63 | + */ |
| 64 | +export function buildServerlessPersistenceError(role: 'control' | 'project' = 'control'): Error { |
| 65 | + const urlVar = role === 'control' ? 'TURSO_DATABASE_URL (or OS_CONTROL_DATABASE_URL)' : 'OS_DATABASE_URL'; |
| 66 | + const tokenVar = role === 'control' ? 'TURSO_AUTH_TOKEN (or OS_CONTROL_DATABASE_AUTH_TOKEN)' : 'OS_DATABASE_AUTH_TOKEN'; |
| 67 | + return new Error( |
| 68 | + `[objectstack/service-cloud] Detected a serverless read-only filesystem ` + |
| 69 | + `(Vercel / AWS Lambda / Netlify) but no persistent database is configured ` + |
| 70 | + `for the ${role === 'control' ? 'control plane' : 'project data plane'}. ` + |
| 71 | + `Set ${urlVar} to a libsql:// URL (recommended on Vercel — Turso is the ` + |
| 72 | + `default ObjectStack pairing for serverless) and ${tokenVar} to the ` + |
| 73 | + `matching auth token. ` + |
| 74 | + `For self-hosted Postgres / MySQL, set the same variable to a ` + |
| 75 | + `postgres:// or mysql:// URL instead. ` + |
| 76 | + `If you have a writable persistent mount (EFS, network volume, …), ` + |
| 77 | + `set OS_DATA_DIR to its path to opt out of this check. ` + |
| 78 | + `File-backed SQLite is rejected on these platforms because /tmp is ` + |
| 79 | + `per-instance and ephemeral, which silently corrupts data across ` + |
| 80 | + `concurrent invocations.`, |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Resolve the canonical default data directory for SQLite / file-backed |
| 86 | + * driver persistence. See module docstring for precedence rules. |
| 87 | + * |
| 88 | + * Throws on serverless platforms unless `OS_DATA_DIR` is set — see |
| 89 | + * {@link buildServerlessPersistenceError} for the rationale. |
| 90 | + * |
| 91 | + * @param env - Optional process-env override, primarily for tests. |
| 92 | + * @returns Absolute filesystem path. Never returns a trailing slash. |
| 93 | + */ |
| 94 | +export function resolveDefaultDataDir(env: NodeJS.ProcessEnv = process.env): string { |
| 95 | + const explicit = env.OS_DATA_DIR?.trim(); |
| 96 | + if (explicit) return resolvePath(explicit); |
| 97 | + |
| 98 | + if (isServerlessReadOnlyFs(env)) { |
| 99 | + throw buildServerlessPersistenceError('control'); |
| 100 | + } |
| 101 | + |
| 102 | + return resolvePath(process.cwd(), '.objectstack/data'); |
| 103 | +} |
0 commit comments