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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions platforms/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,34 @@ export class Redis extends core.Redis {
},
opts?: Omit<RedisConfigCloudflare, "url" | "token">
): 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(
"[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);
Expand Down
33 changes: 22 additions & 11 deletions platforms/nodejs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
// deno-lint-ignore-file

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

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 ?? !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}`,
Expand All @@ -187,21 +197,22 @@ export class Redis extends core.Redis {
* that may use different naming conventions.
*/
static fromEnv(config?: Omit<RedisConfigNodejs, "url" | "token">): 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(
Expand Down
Loading