// deno-lint-ignore-file
import * as core from "../pkg/redis.js";
import { HttpClient, } from "../pkg/http.js";
import { VERSION } from "../version.js";
import "isomorphic-fetch";
// @ts-ignore Deno can't compile
// import https from "https";
// @ts-ignore Deno can't compile
// import http from "http";
// import "isomorphic-fetch";
/**
 * Workaround for nodejs 14, where atob is not included in the standardlib
 */
if (typeof atob === "undefined") {
    global.atob = function (b64) {
        return Buffer.from(b64, "base64").toString("utf-8");
    };
}
/**
 * Serverless redis client for upstash.
 */
export class Redis extends core.Redis {
    constructor(configOrRequester) {
        if ("request" in configOrRequester) {
            super(configOrRequester);
            return;
        }
        if (configOrRequester.url.startsWith(" ") ||
            configOrRequester.url.endsWith(" ") ||
            /\r|\n/.test(configOrRequester.url)) {
            console.warn("The redis url contains whitespace or newline, which can cause errors!");
        }
        if (configOrRequester.token.startsWith(" ") ||
            configOrRequester.token.endsWith(" ") ||
            /\r|\n/.test(configOrRequester.token)) {
            console.warn("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,
        });
        super(client, {
            automaticDeserialization: configOrRequester.automaticDeserialization,
            enableTelemetry: !process.env.UPSTASH_DISABLE_TELEMETRY,
        });
        this.addTelemetry({
            runtime: `node@${process.version}`,
            platform: process.env.VERCEL
                ? "vercel"
                : process.env.AWS_REGION
                    ? "aws"
                    : "unknown",
            sdk: `@upstash/redis@${VERSION}`,
        });
    }
    /**
     * 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) {
        // @ts-ignore process will be defined in node
        if (typeof process?.env === "undefined") {
            throw new Error('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"];
        if (!url) {
            throw new Error("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"];
        if (!token) {
            throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
        }
        return new Redis({ ...config, url, token });
    }
}
