Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-preset-browser-guard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@t3-oss/env-core": patch
---

Server presets no longer crash with `ReferenceError: process is not defined` in browser environments such as Vite apps that do not polyfill `process`.
5 changes: 4 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ export function createEnv<
>(
opts: EnvOptions<TPrefix, TServer, TClient, TShared, TExtends, TFinalSchema>,
): CreateEnv<TFinalSchema, TExtends> {
const runtimeEnv = opts.runtimeEnvStrict ?? opts.runtimeEnv ?? process.env;
const runtimeEnv =
opts.runtimeEnvStrict ??
opts.runtimeEnv ??
(typeof process !== "undefined" ? process.env : {});

const emptyStringAsUndefined = opts.emptyStringAsUndefined ?? false;
if (emptyStringAsUndefined) {
Expand Down
25 changes: 14 additions & 11 deletions packages/core/src/presets-arktype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import type {
WxtEnv,
} from "./presets.ts";

const serverEnv = (): Record<string, string | undefined> =>
typeof process !== "undefined" ? process.env : {};

/**
* Vercel System Environment Variables
* @see https://vercel.com/docs/projects/environment-variables/system-environment-variables#system-environment-variables
Expand Down Expand Up @@ -51,7 +54,7 @@ export const vercel = (): Readonly<VercelEnv> =>
VERCEL_GIT_PREVIOUS_SHA: type("string | undefined"),
VERCEL_GIT_PULL_REQUEST_ID: type("string | undefined"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -77,7 +80,7 @@ export const neonVercel = (): Readonly<NeonVercelEnv> =>
POSTGRES_URL_NO_SSL: type("string.url | undefined"),
POSTGRES_PRISMA_URL: type("string.url | undefined"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -101,7 +104,7 @@ export const supabaseVercel = (): Readonly<SupabaseVercelEnv> =>
NEXT_PUBLIC_SUPABASE_ANON_KEY: type("string | undefined"),
NEXT_PUBLIC_SUPABASE_URL: type("string.url | undefined"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -112,7 +115,7 @@ export const uploadthingV6 = (): Readonly<UploadThingV6Env> =>
server: {
UPLOADTHING_TOKEN: type("string"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -123,7 +126,7 @@ export const uploadthing = (): Readonly<UploadThingEnv> =>
server: {
UPLOADTHING_TOKEN: type("string"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -146,7 +149,7 @@ export const render = (): Readonly<RenderEnv> =>
RENDER_SERVICE_TYPE: type("'web' | 'pserv' | 'cron' | 'worker' | 'static' | undefined"),
RENDER: type("string | undefined"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand Down Expand Up @@ -180,7 +183,7 @@ export const railway = (): Readonly<RailwayEnv> =>
RAILWAY_GIT_REPO_OWNER: type("string | undefined"),
RAILWAY_GIT_COMMIT_MESSAGE: type("string | undefined"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -202,7 +205,7 @@ export const fly = (): Readonly<FlyEnv> =>
FLY_VM_MEMORY_MB: type("string | undefined"),
PRIMARY_REGION: type("string | undefined"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -224,7 +227,7 @@ export const netlify = (): Readonly<NetlifyEnv> =>
SITE_NAME: type("string | undefined"),
SITE_ID: type("string | undefined"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -237,7 +240,7 @@ export const upstashRedis = (): Readonly<UpstashRedisEnv> =>
UPSTASH_REDIS_REST_URL: type("string.url"),
UPSTASH_REDIS_REST_TOKEN: type("string"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -256,7 +259,7 @@ export const coolify = (): Readonly<CoolifyEnv> =>
PORT: type("string | undefined"),
HOST: type("string | undefined"),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand Down
25 changes: 14 additions & 11 deletions packages/core/src/presets-valibot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import type {
WxtEnv,
} from "./presets.ts";

const serverEnv = (): Record<string, string | undefined> =>
typeof process !== "undefined" ? process.env : {};

/**
* Vercel System Environment Variables
* @see https://vercel.com/docs/projects/environment-variables/system-environment-variables#system-environment-variables
Expand Down Expand Up @@ -51,7 +54,7 @@ export const vercel = (): Readonly<VercelEnv> =>
VERCEL_GIT_PREVIOUS_SHA: optional(string()),
VERCEL_GIT_PULL_REQUEST_ID: optional(string()),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -77,7 +80,7 @@ export const neonVercel = (): Readonly<NeonVercelEnv> =>
POSTGRES_URL_NO_SSL: optional(pipe(string(), url())),
POSTGRES_PRISMA_URL: optional(pipe(string(), url())),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -101,7 +104,7 @@ export const supabaseVercel = (): Readonly<SupabaseVercelEnv> =>
NEXT_PUBLIC_SUPABASE_ANON_KEY: optional(string()),
NEXT_PUBLIC_SUPABASE_URL: optional(pipe(string(), url())),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -112,7 +115,7 @@ export const uploadthingV6 = (): Readonly<UploadThingV6Env> =>
server: {
UPLOADTHING_TOKEN: string(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -123,7 +126,7 @@ export const uploadthing = (): Readonly<UploadThingEnv> =>
server: {
UPLOADTHING_TOKEN: string(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -146,7 +149,7 @@ export const render = (): Readonly<RenderEnv> =>
RENDER_SERVICE_TYPE: optional(picklist(["web", "pserv", "cron", "worker", "static"])),
RENDER: optional(string()),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand Down Expand Up @@ -180,7 +183,7 @@ export const railway = (): Readonly<RailwayEnv> =>
RAILWAY_GIT_REPO_OWNER: optional(string()),
RAILWAY_GIT_COMMIT_MESSAGE: optional(string()),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -202,7 +205,7 @@ export const fly = (): Readonly<FlyEnv> =>
FLY_VM_MEMORY_MB: optional(string()),
PRIMARY_REGION: optional(string()),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -224,7 +227,7 @@ export const netlify = (): Readonly<NetlifyEnv> =>
SITE_NAME: optional(string()),
SITE_ID: optional(string()),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -237,7 +240,7 @@ export const upstashRedis = (): Readonly<UpstashRedisEnv> =>
UPSTASH_REDIS_REST_URL: pipe(string(), url()),
UPSTASH_REDIS_REST_TOKEN: string(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -256,7 +259,7 @@ export const coolify = (): Readonly<CoolifyEnv> =>
PORT: optional(string()),
HOST: optional(string()),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand Down
25 changes: 14 additions & 11 deletions packages/core/src/presets-zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import type {
WxtEnv,
} from "./presets.ts";

const serverEnv = (): Record<string, string | undefined> =>
typeof process !== "undefined" ? process.env : {};

/**
* Vercel System Environment Variables
* @see https://vercel.com/docs/projects/environment-variables/system-environment-variables#system-environment-variables
Expand Down Expand Up @@ -51,7 +54,7 @@ export const vercel = (): Readonly<VercelEnv> =>
VERCEL_GIT_PREVIOUS_SHA: z.string().optional(),
VERCEL_GIT_PULL_REQUEST_ID: z.string().optional(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -77,7 +80,7 @@ export const neonVercel = (): Readonly<NeonVercelEnv> =>
POSTGRES_URL_NO_SSL: z.string().url().optional(),
POSTGRES_PRISMA_URL: z.string().url().optional(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -101,7 +104,7 @@ export const supabaseVercel = (): Readonly<SupabaseVercelEnv> =>
NEXT_PUBLIC_SUPABASE_ANON_KEY: z.string().optional(),
NEXT_PUBLIC_SUPABASE_URL: z.string().url().optional(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -112,7 +115,7 @@ export const uploadthingV6 = (): Readonly<UploadThingV6Env> =>
server: {
UPLOADTHING_TOKEN: z.string(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -123,7 +126,7 @@ export const uploadthing = (): Readonly<UploadThingEnv> =>
server: {
UPLOADTHING_TOKEN: z.string(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -146,7 +149,7 @@ export const render = (): Readonly<RenderEnv> =>
RENDER_SERVICE_TYPE: z.enum(["web", "pserv", "cron", "worker", "static"]).optional(),
RENDER: z.string().optional(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand Down Expand Up @@ -180,7 +183,7 @@ export const railway = (): Readonly<RailwayEnv> =>
RAILWAY_GIT_REPO_OWNER: z.string().optional(),
RAILWAY_GIT_COMMIT_MESSAGE: z.string().optional(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -202,7 +205,7 @@ export const fly = (): Readonly<FlyEnv> =>
FLY_VM_MEMORY_MB: z.string().optional(),
PRIMARY_REGION: z.string().optional(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -224,7 +227,7 @@ export const netlify = (): Readonly<NetlifyEnv> =>
SITE_NAME: z.string().optional(),
SITE_ID: z.string().optional(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -237,7 +240,7 @@ export const upstashRedis = (): Readonly<UpstashRedisEnv> =>
UPSTASH_REDIS_REST_URL: z.string().url(),
UPSTASH_REDIS_REST_TOKEN: z.string(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand All @@ -256,7 +259,7 @@ export const coolify = (): Readonly<CoolifyEnv> =>
PORT: z.string().optional(),
HOST: z.string().optional(),
},
runtimeEnv: process.env,
runtimeEnv: serverEnv(),
});

/**
Expand Down
11 changes: 10 additions & 1 deletion packages/core/test/smoke-arktype.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type } from "arktype";
import { describe, expect, expectTypeOf, test, vi } from "vitest";
import { createEnv } from "../src/index.ts";
import { uploadthing } from "../src/presets-arktype.ts";
import { uploadthing, vercel } from "../src/presets-arktype.ts";

function ignoreErrors(cb: () => void) {
try {
Expand Down Expand Up @@ -804,3 +804,12 @@ test("with built-in preset", () => {
expect(env.FOO).toBe("bar");
expect(env.UPLOADTHING_TOKEN).toBe("token");
});

test("server presets do not throw when process is not defined", () => {
vi.stubGlobal("process", undefined);
try {
expect(() => vercel()).not.toThrow();
} finally {
vi.unstubAllGlobals();
}
});
11 changes: 10 additions & 1 deletion packages/core/test/smoke-valibot.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as v from "valibot";
import { describe, expect, expectTypeOf, test, vi } from "vitest";
import { createEnv } from "../src/index.ts";
import { uploadthing } from "../src/presets-valibot.ts";
import { uploadthing, vercel } from "../src/presets-valibot.ts";

function ignoreErrors(cb: () => void) {
try {
Expand Down Expand Up @@ -803,3 +803,12 @@ test("with built-in preset", () => {
expect(env.FOO).toBe("bar");
expect(env.UPLOADTHING_TOKEN).toBe("token");
});

test("server presets do not throw when process is not defined", () => {
vi.stubGlobal("process", undefined);
try {
expect(() => vercel()).not.toThrow();
} finally {
vi.unstubAllGlobals();
}
});
Loading