Skip to content

Commit 8f4f0dd

Browse files
ytkimirtiCahidArda
andauthored
fix: improve env variable access (#1399)
* fix: avoid throwing error if process variable is not defined in the default client * fix: improve error message for when env is missing in cloudflare --------- Co-authored-by: CahidArda <[email protected]>
1 parent 31cc261 commit 8f4f0dd

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

platforms/cloudflare.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,34 @@ export class Redis extends core.Redis {
124124
},
125125
opts?: Omit<RedisConfigCloudflare, "url" | "token">
126126
): Redis {
127-
// @ts-expect-error These will be defined by cloudflare
128-
const url = env?.UPSTASH_REDIS_REST_URL ?? UPSTASH_REDIS_REST_URL;
127+
const url =
128+
env?.UPSTASH_REDIS_REST_URL ??
129+
// @ts-expect-error These will be defined by cloudflare
130+
(typeof UPSTASH_REDIS_REST_URL === "string"
131+
? // @ts-expect-error These will be defined by cloudflare
132+
UPSTASH_REDIS_REST_URL
133+
: undefined);
129134

130-
// @ts-expect-error These will be defined by cloudflare
131-
const token = env?.UPSTASH_REDIS_REST_TOKEN ?? UPSTASH_REDIS_REST_TOKEN;
135+
const token =
136+
env?.UPSTASH_REDIS_REST_TOKEN ??
137+
// @ts-expect-error These will be defined by cloudflare
138+
(typeof UPSTASH_REDIS_REST_TOKEN === "string"
139+
? // @ts-expect-error These will be defined by cloudflare
140+
UPSTASH_REDIS_REST_TOKEN
141+
: undefined);
132142

133-
if (!url) {
134-
console.warn(
135-
"[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_URL`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_URL`"
136-
);
137-
}
138-
if (!token) {
143+
const messageInfo =
144+
!url && !token
145+
? "Unable to find environment variables: `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`"
146+
: url
147+
? token
148+
? undefined
149+
: "Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`"
150+
: "Unable to find environment variable: `UPSTASH_REDIS_REST_URL`";
151+
152+
if (messageInfo) {
139153
console.warn(
140-
"[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`"
154+
`[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`
141155
);
142156
}
143157
return new Redis({ ...opts, url, token }, env);

platforms/nodejs.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/ban-ts-comment */
21
// deno-lint-ignore-file
32

43
import type { HttpClientConfig, Requester, RequesterConfig } from "../pkg/http";
@@ -147,22 +146,33 @@ export class Redis extends core.Redis {
147146
readYourWrites: configOrRequester.readYourWrites,
148147
});
149148

149+
const safeEnv: Record<string, string | undefined> =
150+
typeof process === "object" && process && typeof process.env === "object" && process.env
151+
? process.env
152+
: {};
153+
150154
super(client, {
151155
automaticDeserialization: configOrRequester.automaticDeserialization,
152-
enableTelemetry: configOrRequester.enableTelemetry ?? !process.env.UPSTASH_DISABLE_TELEMETRY,
156+
enableTelemetry: configOrRequester.enableTelemetry ?? !safeEnv.UPSTASH_DISABLE_TELEMETRY,
153157
latencyLogging: configOrRequester.latencyLogging,
154158
enableAutoPipelining: configOrRequester.enableAutoPipelining,
155159
});
156160

161+
const nodeVersion = typeof process === "object" && process ? process.version : undefined;
162+
157163
this.addTelemetry({
158164
runtime:
159165
// @ts-expect-error to silence compiler
160-
typeof EdgeRuntime === "string" ? "edge-light" : `node@${process.version}`,
161-
platform: process.env.UPSTASH_CONSOLE
166+
typeof EdgeRuntime === "string"
167+
? "edge-light"
168+
: nodeVersion
169+
? `node@${nodeVersion}`
170+
: "unknown",
171+
platform: safeEnv.UPSTASH_CONSOLE
162172
? "console"
163-
: process.env.VERCEL
173+
: safeEnv.VERCEL
164174
? "vercel"
165-
: process.env.AWS_REGION
175+
: safeEnv.AWS_REGION
166176
? "aws"
167177
: "unknown",
168178
sdk: `@upstash/redis@${VERSION}`,
@@ -187,21 +197,22 @@ export class Redis extends core.Redis {
187197
* that may use different naming conventions.
188198
*/
189199
static fromEnv(config?: Omit<RedisConfigNodejs, "url" | "token">): Redis {
190-
// @ts-ignore process will be defined in node
191-
192-
if (process.env === undefined) {
200+
if (
201+
typeof process !== "object" ||
202+
!process ||
203+
typeof process.env !== "object" ||
204+
!process.env
205+
) {
193206
throw new TypeError(
194207
'[Upstash Redis] Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please import from "@upstash/redis/cloudflare" instead'
195208
);
196209
}
197210

198-
// @ts-ignore process will be defined in node
199211
const url = process.env.UPSTASH_REDIS_REST_URL || process.env.KV_REST_API_URL;
200212
if (!url) {
201213
console.warn("[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
202214
}
203215

204-
// @ts-ignore process will be defined in node
205216
const token = process.env.UPSTASH_REDIS_REST_TOKEN || process.env.KV_REST_API_TOKEN;
206217
if (!token) {
207218
console.warn(

0 commit comments

Comments
 (0)