UNPKG

@upstash/redis

Version:

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

114 lines (112 loc) 4.09 kB
import { HttpClient, Redis, VERSION, error_exports } from "./chunk-2BA3VA6P.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 }); super(client, { automaticDeserialization: configOrRequester.automaticDeserialization, enableTelemetry: !process.env.UPSTASH_DISABLE_TELEMETRY, latencyLogging: configOrRequester.latencyLogging, enableAutoPipelining: configOrRequester.enableAutoPipelining }); this.addTelemetry({ runtime: ( // @ts-expect-error to silence compiler typeof EdgeRuntime === "string" ? "edge-light" : `node@${process.version}` ), platform: process.env.UPSTASH_CONSOLE ? "console" : process.env.VERCEL ? "vercel" : process.env.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 `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` from * your environment using `process.env`. */ static fromEnv(config) { if (process.env === void 0) { 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 };