@@ -33,14 +33,18 @@ export interface InMemoryDriverConfig {
3333 logger ?: Logger ;
3434 /**
3535 * Persistence configuration. Defaults to `'auto'`.
36- * - `'auto'` (default) — Auto-detect environment (browser → localStorage, Node.js → file)
36+ * - `'auto'` (default) — Auto-detect environment (browser → localStorage, Node.js → file, serverless → disabled )
3737 * - `'file'` — File-system persistence with defaults (Node.js only)
3838 * - `'local'` — localStorage persistence with defaults (Browser only)
3939 * - `{ type: 'file', path?: string, autoSaveInterval?: number }` — File-system with options
4040 * - `{ type: 'local', key?: string }` — localStorage with options
4141 * - `{ type: 'auto', path?: string, key?: string, autoSaveInterval?: number }` — Auto-detect with options
4242 * - `{ adapter: PersistenceAdapterInterface }` — Custom adapter
4343 * - `false` — Disable persistence (pure in-memory)
44+ *
45+ * ⚠️ In serverless environments (Vercel, AWS Lambda, Netlify, etc.),
46+ * auto mode disables file persistence to prevent silent data loss.
47+ * Use `persistence: false` or supply a custom adapter for serverless deployments.
4448 */
4549 persistence ?: string | false | {
4650 type ?: 'file' | 'local' | 'auto' ;
@@ -932,10 +936,52 @@ export class InMemoryDriver implements DriverInterface {
932936 return typeof globalThis . localStorage !== 'undefined' ;
933937 }
934938
939+ /**
940+ * Detect whether the current runtime is a serverless/edge environment.
941+ *
942+ * Checks well-known environment variables set by serverless platforms:
943+ * - `VERCEL` / `VERCEL_ENV` — Vercel Functions / Edge
944+ * - `AWS_LAMBDA_FUNCTION_NAME` — AWS Lambda
945+ * - `NETLIFY` — Netlify Functions
946+ * - `FUNCTIONS_WORKER_RUNTIME` — Azure Functions
947+ * - `K_SERVICE` — Google Cloud Run / Cloud Functions
948+ * - `FUNCTION_TARGET` — Google Cloud Functions (Node.js)
949+ * - `DENO_DEPLOYMENT_ID` — Deno Deploy
950+ *
951+ * Returns `false` when `process` or `process.env` is unavailable
952+ * (e.g. browser or edge runtimes without a Node.js process object).
953+ */
954+ private isServerlessEnvironment ( ) : boolean {
955+ if ( typeof globalThis . process === 'undefined' || ! globalThis . process . env ) {
956+ return false ;
957+ }
958+ const env = globalThis . process . env ;
959+ return ! ! (
960+ env . VERCEL ||
961+ env . VERCEL_ENV ||
962+ env . AWS_LAMBDA_FUNCTION_NAME ||
963+ env . NETLIFY ||
964+ env . FUNCTIONS_WORKER_RUNTIME ||
965+ env . K_SERVICE ||
966+ env . FUNCTION_TARGET ||
967+ env . DENO_DEPLOYMENT_ID
968+ ) ;
969+ }
970+
971+ private static readonly SERVERLESS_PERSISTENCE_WARNING =
972+ 'Serverless environment detected — file-system persistence is disabled in auto mode. ' +
973+ 'Data will NOT be persisted across function invocations. ' +
974+ 'Set persistence: false to silence this warning, or provide a custom adapter ' +
975+ '(e.g. Upstash Redis, Vercel KV) via persistence: { adapter: yourAdapter }.' ;
976+
935977 /**
936978 * Initialize the persistence adapter based on configuration.
937979 * Defaults to 'auto' when persistence is not specified.
938980 * Use `persistence: false` to explicitly disable persistence.
981+ *
982+ * In serverless environments (Vercel, AWS Lambda, etc.), auto mode disables
983+ * file-system persistence and emits a warning. Use `persistence: false` or
984+ * supply a custom adapter for serverless-safe operation.
939985 */
940986 private async initPersistence ( ) : Promise < void > {
941987 const persistence = this . config . persistence === undefined ? 'auto' : this . config . persistence ;
@@ -947,6 +993,8 @@ export class InMemoryDriver implements DriverInterface {
947993 const { LocalStoragePersistenceAdapter } = await import ( './persistence/local-storage-adapter.js' ) ;
948994 this . persistenceAdapter = new LocalStoragePersistenceAdapter ( ) ;
949995 this . logger . debug ( 'Auto-detected browser environment, using localStorage persistence' ) ;
996+ } else if ( this . isServerlessEnvironment ( ) ) {
997+ this . logger . warn ( InMemoryDriver . SERVERLESS_PERSISTENCE_WARNING ) ;
950998 } else {
951999 const { FileSystemPersistenceAdapter } = await import ( './persistence/file-adapter.js' ) ;
9521000 this . persistenceAdapter = new FileSystemPersistenceAdapter ( ) ;
@@ -971,6 +1019,8 @@ export class InMemoryDriver implements DriverInterface {
9711019 key : persistence . key ,
9721020 } ) ;
9731021 this . logger . debug ( 'Auto-detected browser environment, using localStorage persistence' ) ;
1022+ } else if ( this . isServerlessEnvironment ( ) ) {
1023+ this . logger . warn ( InMemoryDriver . SERVERLESS_PERSISTENCE_WARNING ) ;
9741024 } else {
9751025 const { FileSystemPersistenceAdapter } = await import ( './persistence/file-adapter.js' ) ;
9761026 this . persistenceAdapter = new FileSystemPersistenceAdapter ( {
0 commit comments