-
Notifications
You must be signed in to change notification settings - Fork 87
fix: improve env variable access #1399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR improves environment variable access in both the Node.js and Cloudflare clients to prevent runtime errors when global variables are undefined or inaccessible. The changes add defensive checks before accessing process.env in Node.js and global Cloudflare variables, falling back to safe defaults when these variables don't exist.
Key Changes:
- Added
safeEnvwrapper in Node.js client to safely accessprocess.envwith fallback to empty object - Modified Cloudflare client to check if global variables exist using
typeofchecks before accessing them - Updated all environment variable references to use the safe accessor patterns
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| platforms/nodejs.ts | Introduces safeEnv wrapper to safely access process.env in both constructor and fromEnv() method, preventing errors in non-Node.js environments |
| platforms/cloudflare.ts | Adds type checks before accessing global UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN variables to prevent undefined errors |
π‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
9817a78 to
07fde87
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
π‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
π‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
0e60ef8 to
f18ba1e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
platforms/nodejs.ts:222
- The new environment variable safety checks in the constructor and
fromEnv()method lack test coverage. Consider adding tests that verify:
- Behavior when
processis undefined - Behavior when
process.envis undefined - Behavior when environment variables exist and are valid
- Correct runtime detection (node vs edge-light vs unknown)
- Warning messages are logged correctly when variables are missing
This is especially important since these changes fix critical bugs that were causing runtime errors.
const safeEnv: Record<string, string | undefined> =
typeof process === "object" && process && typeof process.env === "object" && process.env
? process.env
: {};
super(client, {
automaticDeserialization: configOrRequester.automaticDeserialization,
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"
: nodeVersion
? `node@${nodeVersion}`
: "unknown",
platform: safeEnv.UPSTASH_CONSOLE
? "console"
: safeEnv.VERCEL
? "vercel"
: safeEnv.AWS_REGION
? "aws"
: "unknown",
sdk: `@upstash/redis@${VERSION}`,
});
if (this.enableAutoPipelining) {
return this.autoPipeline();
}
}
/**
* Create a new Upstash Redis instance from environment variables.
*
* Use this to automatically load connection secrets from your environment
* variables. For instance when using the Vercel integration.
*
* This tries to load connection details from your environment using `process.env`:
* - URL: `UPSTASH_REDIS_REST_URL` or fallback to `KV_REST_API_URL`
* - Token: `UPSTASH_REDIS_REST_TOKEN` or fallback to `KV_REST_API_TOKEN`
*
* The fallback variables provide compatibility with Vercel KV and other platforms
* that may use different naming conventions.
*/
static fromEnv(config?: Omit<RedisConfigNodejs, "url" | "token">): Redis {
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'
);
}
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`");
}
const token = process.env.UPSTASH_REDIS_REST_TOKEN || process.env.KV_REST_API_TOKEN;
if (!token) {
console.warn(
"[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`"
);
}
return new Redis({ ...config, url, token });
π‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
tests failing because we don't have access to the runners anymore, updating the workflows here #1398 Let's merge that PR and update this one |
|
let's get a canary release and test with browser before merging |
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Adoption](https://docs.renovatebot.com/merge-confidence/) | [Passing](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---|---|---| | [@upstash/redis](https://redirect.github.com/upstash/upstash-redis) | [`1.35.7` -> `1.35.8`](https://renovatebot.com/diffs/npm/@upstash%2fredis/1.35.7/1.35.8) |  |  |  |  | --- ### Release Notes <details> <summary>upstash/upstash-redis (@​upstash/redis)</summary> ### [`v1.35.8`](https://redirect.github.com/upstash/redis-js/releases/tag/v1.35.8) [Compare Source](https://redirect.github.com/upstash/upstash-redis/compare/v1.35.7...v1.35.8) #### What's Changed - DX-2280: Remove large-group runners by [@​CahidArda](https://redirect.github.com/CahidArda) in [upstash/redis-js#1398](https://redirect.github.com/upstash/redis-js/pull/1398) - fix: bump next by [@​CahidArda](https://redirect.github.com/CahidArda) in [upstash/redis-js#1400](https://redirect.github.com/upstash/redis-js/pull/1400) - fix: improve env variable access by [@​ytkimirti](https://redirect.github.com/ytkimirti) in [upstash/redis-js#1399](https://redirect.github.com/upstash/redis-js/pull/1399) **Full Changelog**: <upstash/redis-js@v1.35.7...v1.35.8> </details> --- ### Configuration π **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). π¦ **Automerge**: Enabled. β» **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. π **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/inabagumi/mini-apps). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi40Mi4yIiwidXBkYXRlZEluVmVyIjoiNDIuNDIuMiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiYm90OiByZW5vdmF0ZSJdfQ==--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Adoption](https://docs.renovatebot.com/merge-confidence/) | [Passing](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---|---|---| | [@upstash/redis](https://redirect.github.com/upstash/upstash-redis) | [`1.35.7` -> `1.35.8`](https://renovatebot.com/diffs/npm/@upstash%2fredis/1.35.7/1.35.8) |  |  |  |  | --- ### Release Notes <details> <summary>upstash/upstash-redis (@​upstash/redis)</summary> ### [`v1.35.8`](https://redirect.github.com/upstash/redis-js/releases/tag/v1.35.8) [Compare Source](https://redirect.github.com/upstash/upstash-redis/compare/v1.35.7...v1.35.8) #### What's Changed - DX-2280: Remove large-group runners by [@​CahidArda](https://redirect.github.com/CahidArda) in [upstash/redis-js#1398](https://redirect.github.com/upstash/redis-js/pull/1398) - fix: bump next by [@​CahidArda](https://redirect.github.com/CahidArda) in [upstash/redis-js#1400](https://redirect.github.com/upstash/redis-js/pull/1400) - fix: improve env variable access by [@​ytkimirti](https://redirect.github.com/ytkimirti) in [upstash/redis-js#1399](https://redirect.github.com/upstash/redis-js/pull/1399) **Full Changelog**: <upstash/redis-js@v1.35.7...v1.35.8> </details> --- ### Configuration π **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). π¦ **Automerge**: Enabled. β» **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. π **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/inabagumi/shinju-date). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi40Mi4yIiwidXBkYXRlZEluVmVyIjoiNDIuNDIuMiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiYm90OiByZW5vdmF0ZSJdfQ==--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
In the cloudflare client, the sdk was trying to access two global variables that did not exist. This was causing the client to throw a variable undefined error instead of logging a warning like our other sdks.
Also using the redis client in an environment that does not have the
processvariable whould throw an error because we were attempting to access theUPSTASH_REDIS_REST_URLvariables.This PR fixes these issues while maintaining backwards compatibility.