UNPKG

@upstash/redis

Version:

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

98 lines (96 loc) 3.53 kB
import { HttpClient, Redis, VERSION, error_exports } from "./chunk-4AG3JMXU.mjs"; // platforms/cloudflare.ts var Redis2 = class _Redis extends Redis { /** * Create a new redis client * * @example * ```typescript * const redis = new Redis({ * url: "<UPSTASH_REDIS_REST_URL>", * token: "<UPSTASH_REDIS_REST_TOKEN>", * }); * ``` */ constructor(config, env) { if (!config.url) { console.warn( `[Upstash Redis] The 'url' property is missing or undefined in your Redis config.` ); } else if (config.url.startsWith(" ") || config.url.endsWith(" ") || /\r|\n/.test(config.url)) { console.warn( "[Upstash Redis] The redis url contains whitespace or newline, which can cause errors!" ); } if (!config.token) { console.warn( `[Upstash Redis] The 'token' property is missing or undefined in your Redis config.` ); } else if (config.token.startsWith(" ") || config.token.endsWith(" ") || /\r|\n/.test(config.token)) { console.warn( "[Upstash Redis] The redis token contains whitespace or newline, which can cause errors!" ); } const client = new HttpClient({ retry: config.retry, baseUrl: config.url, headers: { authorization: `Bearer ${config.token}` }, responseEncoding: config.responseEncoding, signal: config.signal, keepAlive: config.keepAlive, readYourWrites: config.readYourWrites }); super(client, { enableTelemetry: config.enableTelemetry ?? !env?.UPSTASH_DISABLE_TELEMETRY, automaticDeserialization: config.automaticDeserialization, latencyLogging: config.latencyLogging, enableAutoPipelining: config.enableAutoPipelining }); this.addTelemetry({ platform: "cloudflare", sdk: `@upstash/redis@${VERSION}` }); if (this.enableAutoPipelining) { return this.autoPipeline(); } } /* * Create a new Upstash Redis instance from environment variables on cloudflare. * * This tries to load `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` from * the global namespace * * If you are using a module worker, please pass in the `env` object. * ```ts * const redis = Redis.fromEnv(env) * ``` */ static fromEnv(env, opts) { 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 ) : void 0); 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 ) : void 0); const messageInfo = !url && !token ? "Unable to find environment variables: `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`" : url ? token ? void 0 : "Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`" : "Unable to find environment variable: `UPSTASH_REDIS_REST_URL`"; if (messageInfo) { console.warn( `[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); } }; export { Redis2 as Redis, error_exports as errors };