From f18ba1e40290a77620d312ec2a989ba2567e8fd9 Mon Sep 17 00:00:00 2001 From: ytkimirti Date: Fri, 5 Dec 2025 16:56:29 +0300 Subject: [PATCH 1/2] fix: avoid throwing error if process variable is not defined in the default client --- platforms/cloudflare.ts | 18 ++++++++++++++---- platforms/nodejs.ts | 33 ++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/platforms/cloudflare.ts b/platforms/cloudflare.ts index 908736d6..cb4f4a6f 100644 --- a/platforms/cloudflare.ts +++ b/platforms/cloudflare.ts @@ -124,11 +124,21 @@ export class Redis extends core.Redis { }, opts?: Omit ): Redis { - // @ts-expect-error These will be defined by cloudflare - const url = env?.UPSTASH_REDIS_REST_URL ?? UPSTASH_REDIS_REST_URL; + const url = + env?.UPSTASH_REDIS_REST_URL ?? + // @ts-expect-error These will be defined by cloudflare + (typeof UPSTASH_REDIS_REST_URL === "string" + ? // @ts-expect-error These will be defined by cloudflare + UPSTASH_REDIS_REST_URL + : undefined); - // @ts-expect-error These will be defined by cloudflare - const token = env?.UPSTASH_REDIS_REST_TOKEN ?? UPSTASH_REDIS_REST_TOKEN; + const token = + env?.UPSTASH_REDIS_REST_TOKEN ?? + // @ts-expect-error These will be defined by cloudflare + (typeof UPSTASH_REDIS_REST_TOKEN === "string" + ? // @ts-expect-error These will be defined by cloudflare + UPSTASH_REDIS_REST_TOKEN + : undefined); if (!url) { console.warn( diff --git a/platforms/nodejs.ts b/platforms/nodejs.ts index 201c0db5..67372d32 100644 --- a/platforms/nodejs.ts +++ b/platforms/nodejs.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/ban-ts-comment */ // deno-lint-ignore-file import type { HttpClientConfig, Requester, RequesterConfig } from "../pkg/http"; @@ -147,22 +146,33 @@ export class Redis extends core.Redis { readYourWrites: configOrRequester.readYourWrites, }); + const safeEnv: Record = + typeof process === "object" && process && typeof process.env === "object" && process.env + ? process.env + : {}; + super(client, { automaticDeserialization: configOrRequester.automaticDeserialization, - enableTelemetry: configOrRequester.enableTelemetry ?? !process.env.UPSTASH_DISABLE_TELEMETRY, + enableTelemetry: configOrRequester.enableTelemetry ?? !safeEnv.UPSTASH_DISABLE_TELEMETRY, latencyLogging: configOrRequester.latencyLogging, enableAutoPipelining: configOrRequester.enableAutoPipelining, }); + const nodeVersion = typeof process === "object" && process ? process.version : undefined; + this.addTelemetry({ runtime: // @ts-expect-error to silence compiler - typeof EdgeRuntime === "string" ? "edge-light" : `node@${process.version}`, - platform: process.env.UPSTASH_CONSOLE + typeof EdgeRuntime === "string" + ? "edge-light" + : nodeVersion + ? `node@${nodeVersion}` + : "unknown", + platform: safeEnv.UPSTASH_CONSOLE ? "console" - : process.env.VERCEL + : safeEnv.VERCEL ? "vercel" - : process.env.AWS_REGION + : safeEnv.AWS_REGION ? "aws" : "unknown", sdk: `@upstash/redis@${VERSION}`, @@ -187,21 +197,22 @@ export class Redis extends core.Redis { * that may use different naming conventions. */ static fromEnv(config?: Omit): Redis { - // @ts-ignore process will be defined in node - - if (process.env === undefined) { + if ( + typeof process !== "object" || + !process || + typeof process.env !== "object" || + !process.env + ) { throw new TypeError( '[Upstash Redis] Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please import from "@upstash/redis/cloudflare" instead' ); } - // @ts-ignore process will be defined in node const url = process.env.UPSTASH_REDIS_REST_URL || process.env.KV_REST_API_URL; if (!url) { console.warn("[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_URL`"); } - // @ts-ignore process will be defined in node const token = process.env.UPSTASH_REDIS_REST_TOKEN || process.env.KV_REST_API_TOKEN; if (!token) { console.warn( From f3b08587a35faa2a623d8dffb5610db6b7e65cf7 Mon Sep 17 00:00:00 2001 From: ytkimirti Date: Mon, 8 Dec 2025 11:01:24 +0300 Subject: [PATCH 2/2] fix: improve error message for when env is missing in cloudflare --- platforms/cloudflare.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/platforms/cloudflare.ts b/platforms/cloudflare.ts index cb4f4a6f..53ce8728 100644 --- a/platforms/cloudflare.ts +++ b/platforms/cloudflare.ts @@ -140,14 +140,18 @@ export class Redis extends core.Redis { UPSTASH_REDIS_REST_TOKEN : undefined); - if (!url) { - console.warn( - "[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_URL`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_URL`" - ); - } - if (!token) { + const messageInfo = + !url && !token + ? "Unable to find environment variables: `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`" + : url + ? token + ? undefined + : "Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`" + : "Unable to find environment variable: `UPSTASH_REDIS_REST_URL`"; + + if (messageInfo) { console.warn( - "[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`" + `[Upstash Redis] ${messageInfo}. Please add it via \`wrangler secret put ${url ? "UPSTASH_REDIS_REST_TOKEN" : "UPSTASH_REDIS_REST_URL"}\` and provide it as an argument to the \`Redis.fromEnv\` function` ); } return new Redis({ ...opts, url, token }, env);