Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Change Env usage from process.env to import (#973)
Ivan Starkov3e39df697002
diff --git a/apps/designer/app/auth/login-button.tsx b/apps/designer/app/auth/login-button.tsxindex 3aeddef771..98e830b375 100644--- a/apps/designer/app/auth/login-button.tsx+++ b/apps/designer/app/auth/login-button.tsx@@ -3,7 +3,7 @@ import { Button, Flex, Tooltip } from "@webstudio-is/design-system";import env from "~/shared/env";import { theme } from "@webstudio-is/design-system";-const isPreviewEnvironment = env.VERCEL_ENV === "preview";+const isPreviewEnvironment = env.DEPLOYMENT_ENVIRONMENT === "preview";export const LoginButton = ({children,diff --git a/apps/designer/app/env.server.ts b/apps/designer/app/env/env.public.server.tssimilarity index 56%rename from apps/designer/app/env.server.tsrename to apps/designer/app/env/env.public.server.tsindex 1c26652878..2d81595064 100644--- a/apps/designer/app/env.server.ts+++ b/apps/designer/app/env/env.public.server.ts@@ -1,8 +1,15 @@-// Environment variables we want to send to the UI inlined in the document.-// Never use a private key here, because it will become public.+/**+ * .server.ts extension is required for this file to work with env-getter utility+ **/+import serverEnv from "./env.server";++/**+ * Environment variables we want to send to the UI inlined in the document.+ * Never use a private key here, because it will become public.+ **/const env = {SENTRY_DSN: process.env.SENTRY_DSN,- VERCEL_ENV: process.env.VERCEL_ENV,+ DEPLOYMENT_ENVIRONMENT: serverEnv.DEPLOYMENT_ENVIRONMENT,DEBUG: process.env.DEBUG,FEATURES: process.env.FEATURES,DESIGNER_HOST: process.env.DESIGNER_HOST,@@ -15,4 +22,4 @@ const env = {export default env;-export type Env = typeof env;+export type PublicEnv = typeof env;diff --git a/apps/designer/app/env/env.server.ts b/apps/designer/app/env/env.server.tsnew file mode 100644index 0000000000..0daabb54e2--- /dev/null+++ b/apps/designer/app/env/env.server.ts@@ -0,0 +1,43 @@+// @todo zod parse env, make explicit types i.e+const env = {+ // Authentication+ DEV_LOGIN: process.env.DEV_LOGIN,+ GH_CLIENT_ID: process.env.GH_CLIENT_ID,+ GH_CLIENT_SECRET: process.env.GH_CLIENT_SECRET,+ GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,+ GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,++ // Secret session key, context encode+ AUTH_SECRET: process.env.AUTH_SECRET,++ // Publishing+ PUBLISHER_ENDPOINT: process.env.PUBLISHER_ENDPOINT,+ PUBLISHER_TOKEN: process.env.PUBLISHER_TOKEN,++ // DEPLOYMENT_ENVIRONMENT developement | preview | production+ DEPLOYMENT_ENVIRONMENT: process.env.DEPLOYMENT_ENVIRONMENT,+ DEPLOYMENT_URL: process.env.DEPLOYMENT_URL,++ // Trpc on SaaS+ TRPC_SERVER_URL: process.env.TRPC_SERVER_URL,+ TRPC_SERVER_API_TOKEN: process.env.TRPC_SERVER_API_TOKEN,++ // Canvas build logic+ BUILD_ORIGIN: process.env.BUILD_ORIGIN,+ BUILD_REQUIRE_SUBDOMAIN: process.env.BUILD_REQUIRE_SUBDOMAIN,++ PORT: process.env.PORT,+};++export type ServerEnv = typeof env;++if (process.env.VERCEL !== undefined) {+ if (env.DEPLOYMENT_ENVIRONMENT === undefined) {+ env.DEPLOYMENT_ENVIRONMENT = process.env.VERCEL_ENV;+ }+ if (env.DEPLOYMENT_URL === undefined) {+ env.DEPLOYMENT_URL = process.env.VERCEL_URL;+ }+}++export default env;diff --git a/apps/designer/app/routes/$.tsx b/apps/designer/app/routes/$.tsxindex 333df2a05a..c2c9310c15 100644--- a/apps/designer/app/routes/$.tsx+++ b/apps/designer/app/routes/$.tsx@@ -4,7 +4,7 @@ import { useLoaderData } from "@remix-run/react";import djb2a from "djb2a";import { InstanceRoot, Root } from "@webstudio-is/react-sdk";import { loadCanvasData } from "~/shared/db";-import env, { type Env } from "~/env.server";+import env, { type PublicEnv } from "~/env/env.public.server";import { sentryException } from "~/shared/sentry";import { Canvas } from "~/canvas";import { ErrorMessage } from "~/shared/error";@@ -19,7 +19,7 @@ import type { CanvasData } from "@webstudio-is/project";import { customComponents } from "~/canvas/custom-components";import { createContext } from "~/shared/context.server";-type Data = CanvasData & { env: Env; mode: BuildMode };+type Data = CanvasData & { env: PublicEnv; mode: BuildMode };export const dynamicLinks: DynamicLinksFunction<CanvasData> = ({data,diff --git a/apps/designer/app/routes/dashboard.tsx b/apps/designer/app/routes/dashboard.tsxindex 35ca4c2cbc..f28cf21b13 100644--- a/apps/designer/app/routes/dashboard.tsx+++ b/apps/designer/app/routes/dashboard.tsx@@ -1,6 +1,6 @@import type { LoaderArgs, MetaFunction } from "@remix-run/node";import { Root } from "~/shared/remix";-import env from "~/env.server";+import env from "~/env/env.public.server";import { getThemeData } from "~/shared/theme";export const loader = async ({ request }: LoaderArgs) => {diff --git a/apps/designer/app/routes/designer.tsx b/apps/designer/app/routes/designer.tsxindex 944712fb5b..52bae8058c 100644--- a/apps/designer/app/routes/designer.tsx+++ b/apps/designer/app/routes/designer.tsx@@ -1,6 +1,6 @@import type { LoaderArgs, MetaFunction } from "@remix-run/node";import { Root } from "~/shared/remix";-import env from "~/env.server";+import env from "~/env/env.public.server";import { getThemeData } from "~/shared/theme";import type { ShouldRevalidateFunction } from "@remix-run/react";diff --git a/apps/designer/app/routes/login.tsx b/apps/designer/app/routes/login.tsxindex 756a3d865c..06d067aeae 100644--- a/apps/designer/app/routes/login.tsx+++ b/apps/designer/app/routes/login.tsx@@ -1,6 +1,6 @@import type { LoaderArgs, MetaFunction } from "@remix-run/node";import { Root } from "~/shared/remix";-import env from "~/env.server";+import env from "~/env/env.public.server";import { getThemeData } from "~/shared/theme";export const loader = async ({ request }: LoaderArgs) => {diff --git a/apps/designer/app/routes/login/index.tsx b/apps/designer/app/routes/login/index.tsxindex e3af48d559..93451c8a29 100644--- a/apps/designer/app/routes/login/index.tsx+++ b/apps/designer/app/routes/login/index.tsx@@ -1,7 +1,7 @@import { type LoaderArgs, redirect, json } from "@remix-run/node";import { findAuthenticatedUser } from "~/services/auth.server";-import env from "~/env.server";+import env from "~/env/env.server";import { Login, links } from "~/auth";import { useLoginErrorMessage } from "~/shared/session";@@ -36,13 +36,11 @@ export const loader = async ({ request }: LoaderArgs) => {return json({- isDevLogin: process.env.DEV_LOGIN === "true",+ isDevLogin: env.DEV_LOGIN === "true",env,- isGithubEnabled: Boolean(- process.env.GH_CLIENT_ID && process.env.GH_CLIENT_SECRET- ),+ isGithubEnabled: Boolean(env.GH_CLIENT_ID && env.GH_CLIENT_SECRET),isGoogleEnabled: Boolean(- process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET+ env.GOOGLE_CLIENT_ID && env.GOOGLE_CLIENT_SECRET),},{ headers }diff --git a/apps/designer/app/routes/rest/publish.ts b/apps/designer/app/routes/rest/publish.tsindex e825e040c7..0bb4fe6e6e 100644--- a/apps/designer/app/routes/rest/publish.ts+++ b/apps/designer/app/routes/rest/publish.ts@@ -1,5 +1,6 @@import type { ActionArgs } from "@remix-run/node";import { zfd } from "zod-form-data";+import env from "~/env/env.server";import { createContext } from "~/shared/context.server";import * as db from "~/shared/db";@@ -15,12 +16,12 @@ export const action = async ({ request }: ActionArgs) => {const context = await createContext(request);await db.misc.publish({ projectId, domain }, context);- if (process.env.PUBLISHER_ENDPOINT && process.env.PUBLISHER_TOKEN) {+ if (env.PUBLISHER_ENDPOINT && env.PUBLISHER_TOKEN) {const headers = new Headers();- headers.append("X-AUTH-WEBSTUDIO", process.env.PUBLISHER_TOKEN || "");+ headers.append("X-AUTH-WEBSTUDIO", env.PUBLISHER_TOKEN || "");headers.append("Content-Type", "text/plain");const url = new URL(request.url);- const response = await fetch(process.env.PUBLISHER_ENDPOINT, {+ const response = await fetch(env.PUBLISHER_ENDPOINT, {method: "PUT",headers,body: JSON.stringify({diff --git a/apps/designer/app/routes/s/css.ts b/apps/designer/app/routes/s/css.tsindex 997d7c627f..9b1371176e 100644--- a/apps/designer/app/routes/s/css.ts+++ b/apps/designer/app/routes/s/css.ts@@ -1,7 +1,6 @@import { type ActionArgs, json } from "@remix-run/node";-+import env from "~/env/env.public.server";import { getBuildParams } from "~/shared/router-utils";-import env from "~/env.server";import { sentryException } from "~/shared/sentry";import { generateCssText } from "~/shared/css-utils";import { createContext } from "~/shared/context.server";diff --git a/apps/designer/app/services/auth.server.ts b/apps/designer/app/services/auth.server.tsindex 263ef8c33c..c3a9f0de11 100644--- a/apps/designer/app/services/auth.server.ts+++ b/apps/designer/app/services/auth.server.ts@@ -8,11 +8,12 @@ import { sentryException } from "~/shared/sentry";import { AUTH_PROVIDERS } from "~/shared/session";import { authCallbackPath } from "~/shared/router-utils";import { getUserById, User } from "~/shared/db/user.server";+import env from "~/env/env.server";const url =- process.env.DEPLOYMENT_ENVIRONMENT === "production"- ? process.env.DEPLOYMENT_URL- : `http://localhost:${process.env.PORT || 3000}`;+ env.DEPLOYMENT_ENVIRONMENT === "production"+ ? env.DEPLOYMENT_URL+ : `http://localhost:${env.PORT || 3000}`;const strategyCallback = async ({profile,@@ -41,11 +42,11 @@ export const authenticator = new Authenticator<User>(sessionStorage, {throwOnError: true,});-if (process.env.GH_CLIENT_ID && process.env.GH_CLIENT_SECRET) {+if (env.GH_CLIENT_ID && env.GH_CLIENT_SECRET) {const github = new GitHubStrategy({- clientID: process.env.GH_CLIENT_ID,- clientSecret: process.env.GH_CLIENT_SECRET,+ clientID: env.GH_CLIENT_ID,+ clientSecret: env.GH_CLIENT_SECRET,callbackURL: `${url}${authCallbackPath({ provider: "github" })}`,},strategyCallback@@ -53,11 +54,11 @@ if (process.env.GH_CLIENT_ID && process.env.GH_CLIENT_SECRET) {authenticator.use(github, "github");}-if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {+if (env.GOOGLE_CLIENT_ID && env.GOOGLE_CLIENT_SECRET) {const google = new GoogleStrategy({- clientID: process.env.GOOGLE_CLIENT_ID,- clientSecret: process.env.GOOGLE_CLIENT_SECRET,+ clientID: env.GOOGLE_CLIENT_ID,+ clientSecret: env.GOOGLE_CLIENT_SECRET,callbackURL: `${url}${authCallbackPath({ provider: "google" })}`,},strategyCallback@@ -65,12 +66,12 @@ if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {authenticator.use(google, "google");}-if (process.env.DEV_LOGIN === "true") {+if (env.DEV_LOGIN === "true") {authenticator.use(new FormStrategy(async ({ form }) => {const secret = form.get("secret");- if (secret === process.env.AUTH_SECRET?.slice(0, 4)) {+ if (secret === env.AUTH_SECRET?.slice(0, 4)) {try {const user = await db.user.createOrLoginWithDev();return user;diff --git a/apps/designer/app/services/session.server.ts b/apps/designer/app/services/session.server.tsindex 79e30d84f7..c04735112b 100644--- a/apps/designer/app/services/session.server.ts+++ b/apps/designer/app/services/session.server.ts@@ -1,4 +1,5 @@import { createCookieSessionStorage } from "@remix-run/node";+import env from "~/env/env.server";// export the whole sessionStorage objectexport const sessionStorage = createCookieSessionStorage({@@ -8,7 +9,7 @@ export const sessionStorage = createCookieSessionStorage({sameSite: "lax", // this helps with CSRFpath: "/", // remember to add this so the cookie will work in all routeshttpOnly: true, // for security reasons, make this cookie http only- secrets: process.env.AUTH_SECRET ? [process.env.AUTH_SECRET] : undefined, // replace this with an actual secret+ secrets: env.AUTH_SECRET ? [env.AUTH_SECRET] : undefined, // replace this with an actual secretsecure: process.env.NODE_ENV === "production", // enable this in prod only},});diff --git a/apps/designer/app/services/trpc.server.ts b/apps/designer/app/services/trpc.server.tsindex bddf43dc67..f42b9fd99f 100644--- a/apps/designer/app/services/trpc.server.ts+++ b/apps/designer/app/services/trpc.server.ts@@ -1,7 +1,8 @@import { createTrpcProxyServiceClient } from "@webstudio-is/trpc-interface/server";+import env from "~/env/env.server";-const TRPC_SERVER_URL = process.env.TRPC_SERVER_URL;-const TRPC_SERVER_API_TOKEN = process.env.TRPC_SERVER_API_TOKEN;+const TRPC_SERVER_URL = env.TRPC_SERVER_URL;+const TRPC_SERVER_API_TOKEN = env.TRPC_SERVER_API_TOKEN;export const trpcClient = createTrpcProxyServiceClient(TRPC_SERVER_URL !== undefined && TRPC_SERVER_API_TOKEN !== undefineddiff --git a/apps/designer/app/shared/context.server.ts b/apps/designer/app/shared/context.server.tsindex 3669339ef4..221acb33ec 100644--- a/apps/designer/app/shared/context.server.ts+++ b/apps/designer/app/shared/context.server.ts@@ -3,6 +3,7 @@ import { z } from "zod";import * as cryptoJson from "./crypto/crypto-json.server";import { authenticator } from "~/services/auth.server";import { trpcClient } from "~/services/trpc.server";+import env from "~/env/env.server";const AuthReadToken = z.object({projectId: z.string(),@@ -25,10 +26,7 @@ const createAuthorizationContext = async (if (authReadTokenRaw !== undefined) {authReadToken = AuthReadToken.parse(- await cryptoJson.decode(- authReadTokenRaw,- process.env.AUTH_SECRET ?? "NO-SECRET"- )+ await cryptoJson.decode(authReadTokenRaw, env.AUTH_SECRET ?? "NO-SECRET"));}@@ -44,7 +42,7 @@ const createAuthorizationContext = async (};export const createAuthReadToken = async (tokenData: AuthReadToken) => {- return cryptoJson.encode(tokenData, process.env.AUTH_SECRET ?? "NO-SECRET");+ return cryptoJson.encode(tokenData, env.AUTH_SECRET ?? "NO-SECRET");};/**diff --git a/apps/designer/app/shared/env/env-getter.ts b/apps/designer/app/shared/env/env-getter.tsindex 95f9082818..2f102e5944 100644--- a/apps/designer/app/shared/env/env-getter.ts+++ b/apps/designer/app/shared/env/env-getter.ts@@ -1,5 +1,6 @@import { namespace } from "./namespace";-import type { Env } from "~/env.server";+import publicEnv from "~/env/env.public.server";+import type { PublicEnv } from "~/env/env.public.server";// @todo remove once remix has a built-in way// https://github.com/remix-run/remix/discussions/2769@@ -9,10 +10,12 @@ export default new Proxy({get(_target, prop) {if (typeof window === "undefined") {- return prop in process.env ? process.env[prop as keyof Env] : undefined;+ return prop in publicEnv+ ? publicEnv[prop as keyof PublicEnv]+ : undefined;}- const env = (window[namespace as never] ?? {}) as unknown as Env;- return prop in env ? env[prop as keyof Env] : undefined;+ const env = (window[namespace as never] ?? {}) as unknown as PublicEnv;+ return prop in env ? env[prop as keyof PublicEnv] : undefined;},}-) as Env;+) as PublicEnv;diff --git a/apps/designer/app/shared/router-utils/build-params.test.ts b/apps/designer/app/shared/router-utils/build-params.test.tsindex 59cfcb7c4e..382f188040 100644--- a/apps/designer/app/shared/router-utils/build-params.test.ts+++ b/apps/designer/app/shared/router-utils/build-params.test.ts@@ -24,7 +24,9 @@ describe("getBuildOrigin", () => {});test("Local development", () => {- const env = { NODE_ENV: "development" } as const;+ const env = {} as const;+ const saveNodeEnv = process.env.NODE_ENV;+ process.env.NODE_ENV = "development";expect(getBuildOrigin(makeRequest("http://localhost"), env)).toBe("http://localhost");@@ -34,13 +36,14 @@ describe("getBuildOrigin", () => {expect(getBuildOrigin(makeRequest("http://foo.localhost:1234"), env)).toBe("http://localhost:1234");+ process.env.NODE_ENV = saveNodeEnv;});test("Vercel preview", () => {const env = {...process.env,- VERCEL_ENV: "preview",- VERCEL_URL: "bar.foo.com",+ DEPLOYMENT_ENVIRONMENT: "preview",+ DEPLOYMENT_URL: "bar.foo.com",};expect(getBuildOrigin(makeRequest("https://baz.com"), env)).toBe("https://bar.foo.com"@@ -50,7 +53,6 @@ describe("getBuildOrigin", () => {describe("getBuildParams", () => {const env = {- ...process.env,BUILD_ORIGIN: "https://foo.com",};diff --git a/apps/designer/app/shared/router-utils/build-params.ts b/apps/designer/app/shared/router-utils/build-params.tsindex 792ed3a9d4..4d65f286ce 100644--- a/apps/designer/app/shared/router-utils/build-params.ts+++ b/apps/designer/app/shared/router-utils/build-params.ts@@ -1,6 +1,8 @@// These are the utils for manipulating "build" params.// "Build" means user generated content — what user builds.+import serverEnv from "~/env/env.server";+export type BuildMode = "edit" | "preview" | "published";export type BuildParams = ({ pagePath: string } | { pageId: string }) &({ projectId: string } | { projectDomain: string }) & { mode: BuildMode };@@ -31,9 +33,20 @@ const isLocalhost = (host: string) => {return domain === "localhost" || domain.endsWith(".localhost");};+const defaultEnv = {+ BUILD_ORIGIN: serverEnv.BUILD_ORIGIN,+ DEPLOYMENT_ENVIRONMENT: serverEnv.DEPLOYMENT_ENVIRONMENT,+ DEPLOYMENT_URL: serverEnv.DEPLOYMENT_URL,+ BUILD_REQUIRE_SUBDOMAIN: serverEnv.BUILD_REQUIRE_SUBDOMAIN,+};+export const getBuildOrigin = (request: MinimalRequest,- env = process.env+ env: {+ BUILD_ORIGIN?: string;+ DEPLOYMENT_ENVIRONMENT?: string;+ DEPLOYMENT_URL?: string;+ } = defaultEnv): string => {const { BUILD_ORIGIN } = env;if (BUILD_ORIGIN !== undefined && BUILD_ORIGIN !== "") {@@ -42,7 +55,7 @@ export const getBuildOrigin = (// Local development special caseconst host = getRequestHost(request);- if (env.NODE_ENV === "development") {+ if (process.env.NODE_ENV === "development") {if (isLocalhost(host)) {return `http://${host.split(".").pop()}`;}@@ -58,10 +71,11 @@ export const getBuildOrigin = (// Vercel preview special caseif (- (env.VERCEL_ENV === "preview" || env.VERCEL_ENV === "development") &&- typeof env.VERCEL_URL === "string"+ (env.DEPLOYMENT_ENVIRONMENT === "preview" ||+ env.DEPLOYMENT_ENVIRONMENT === "development") &&+ typeof env.DEPLOYMENT_URL === "string") {- return `https://${env.VERCEL_URL}`;+ return `https://${env.DEPLOYMENT_URL}`;}throw new Error("Could not determine user content host");@@ -69,7 +83,12 @@ export const getBuildOrigin = (export const getBuildParams = (request: MinimalRequest,- env = process.env+ env: {+ BUILD_REQUIRE_SUBDOMAIN?: string;+ BUILD_ORIGIN?: string;+ DEPLOYMENT_ENVIRONMENT?: string;+ DEPLOYMENT_URL?: string;+ } = defaultEnv): BuildParams | undefined => {const url = new URL(request.url);diff --git a/apps/designer/app/shared/sentry.ts b/apps/designer/app/shared/sentry.tsindex c5afda9c66..c976ccfb81 100644--- a/apps/designer/app/shared/sentry.ts+++ b/apps/designer/app/shared/sentry.ts@@ -9,7 +9,7 @@ export const initSentry = ({? Sentry.init({dsn: env.SENTRY_DSN,tracesSampleRate: 1.0,- environment: env.VERCEL_ENV || "development",+ environment: env.DEPLOYMENT_ENVIRONMENT || "development",integrations: integrations,}): () => null;