Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ytkimirti
Copy link
Collaborator

  • 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 process variable whould throw an error because we were attempting to access the UPSTASH_REDIS_REST_URL variables.

This PR fixes these issues while maintaining backwards compatibility.

@linear
Copy link

linear bot commented Dec 5, 2025

Copy link

Copilot AI left a 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 safeEnv wrapper in Node.js client to safely access process.env with fallback to empty object
  • Modified Cloudflare client to check if global variables exist using typeof checks 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.

Copy link

Copilot AI left a 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.

Copy link

Copilot AI left a 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.

Copy link

Copilot AI left a 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:
  1. Behavior when process is undefined
  2. Behavior when process.env is undefined
  3. Behavior when environment variables exist and are valid
  4. Correct runtime detection (node vs edge-light vs unknown)
  5. 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.

@CahidArda
Copy link
Contributor

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

@CahidArda
Copy link
Contributor

let's get a canary release and test with browser before merging

@CahidArda CahidArda merged commit 8f4f0dd into main Dec 12, 2025
15 checks passed
@CahidArda CahidArda deleted the DX-2299-safe-env branch December 12, 2025 12:22
renovate bot added a commit to inabagumi/mini-apps that referenced this pull request Dec 12, 2025
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)
|
![age](https://developer.mend.io/api/mc/badges/age/npm/@upstash%2fredis/1.35.8?slim=true)
|
![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@upstash%2fredis/1.35.8?slim=true)
|
![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@upstash%2fredis/1.35.7/1.35.8?slim=true)
|
![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@upstash%2fredis/1.35.7/1.35.8?slim=true)
|

---

### Release Notes

<details>
<summary>upstash/upstash-redis (@&#8203;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
[@&#8203;CahidArda](https://redirect.github.com/CahidArda) in
[upstash/redis-js#1398](https://redirect.github.com/upstash/redis-js/pull/1398)
- fix: bump next by
[@&#8203;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
[@&#8203;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>
renovate bot added a commit to inabagumi/shinju-date that referenced this pull request Dec 12, 2025
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)
|
![age](https://developer.mend.io/api/mc/badges/age/npm/@upstash%2fredis/1.35.8?slim=true)
|
![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@upstash%2fredis/1.35.8?slim=true)
|
![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@upstash%2fredis/1.35.7/1.35.8?slim=true)
|
![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@upstash%2fredis/1.35.7/1.35.8?slim=true)
|

---

### Release Notes

<details>
<summary>upstash/upstash-redis (@&#8203;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
[@&#8203;CahidArda](https://redirect.github.com/CahidArda) in
[upstash/redis-js#1398](https://redirect.github.com/upstash/redis-js/pull/1398)
- fix: bump next by
[@&#8203;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
[@&#8203;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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants