UNPKG

@upstash/redis

Version:

An HTTP/REST based Redis client built on top of Upstash REST API.

120 lines (118 loc) 4.66 kB
import { HttpClient, Redis, VERSION, error_exports } from "./chunk-4AG3JMXU.mjs"; // platforms/nodejs.ts if (typeof atob === "undefined") { global.atob = (b64) => Buffer.from(b64, "base64").toString("utf8"); } var Redis2 = class _Redis extends Redis { /** * Create a new redis client by providing a custom `Requester` implementation * * @example * ```ts * * import { UpstashRequest, Requester, UpstashResponse, Redis } from "@upstash/redis" * * const requester: Requester = { * request: <TResult>(req: UpstashRequest): Promise<UpstashResponse<TResult>> => { * // ... * } * } * * const redis = new Redis(requester) * ``` */ constructor(configOrRequester) { if ("request" in configOrRequester) { super(configOrRequester); return; } if (!configOrRequester.url) { console.warn( `[Upstash Redis] The 'url' property is missing or undefined in your Redis config.` ); } else if (configOrRequester.url.startsWith(" ") || configOrRequester.url.endsWith(" ") || /\r|\n/.test(configOrRequester.url)) { console.warn( "[Upstash Redis] The redis url contains whitespace or newline, which can cause errors!" ); } if (!configOrRequester.token) { console.warn( `[Upstash Redis] The 'token' property is missing or undefined in your Redis config.` ); } else if (configOrRequester.token.startsWith(" ") || configOrRequester.token.endsWith(" ") || /\r|\n/.test(configOrRequester.token)) { console.warn( "[Upstash Redis] The redis token contains whitespace or newline, which can cause errors!" ); } const client = new HttpClient({ baseUrl: configOrRequester.url, retry: configOrRequester.retry, headers: { authorization: `Bearer ${configOrRequester.token}` }, agent: configOrRequester.agent, responseEncoding: configOrRequester.responseEncoding, cache: configOrRequester.cache ?? "no-store", signal: configOrRequester.signal, keepAlive: configOrRequester.keepAlive, readYourWrites: configOrRequester.readYourWrites }); const safeEnv = 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 : void 0; 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) { 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 }); } }; export { Redis2 as Redis, error_exports as errors };