@upstash/redis 1.3.4 → 1.4.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.releaserc ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "branches": [
3
+ {
4
+ "name": "release"
5
+ },
6
+ {
7
+ "name": "main",
8
+ "channel": "next",
9
+ "prerelease": "next"
10
+ }
11
+ ],
12
+ "dryRun": false,
13
+ "ci": true
14
+ }
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Upstash Redis
2
2
 
3
- An HTTP/REST based Redis client built on top of
4
- [Upstash REST API](https://docs.upstash.com/features/restapi).
3
+ `@upstash/redis` is an HTTP/REST based Redis client for typescript, built on top
4
+ of [Upstash REST API](https://docs.upstash.com/features/restapi).
5
5
 
6
6
  [![Tests](https://github.com/upstash/upstash-redis/actions/workflows/tests.yaml/badge.svg)](https://github.com/upstash/upstash-redis/actions/workflows/tests.yaml)
7
7
  ![npm (scoped)](https://img.shields.io/npm/v/@upstash/redis)
@@ -23,6 +23,17 @@ See
23
23
  [the list of APIs](https://docs.upstash.com/features/restapi#rest---redis-api-compatibility)
24
24
  supported.
25
25
 
26
+ ## Upgrading to v1.4.0 **(ReferenceError: fetch is not defined)**
27
+
28
+ If you are running on nodejs v17 and earlier, `fetch` will not be natively
29
+ supported. Platforms like Vercel, Netlify, Deno, Fastly etc. provide a polyfill
30
+ for you. But if you are running on bare node, you need to either specify a
31
+ polyfill yourself or change the import path to:
32
+
33
+ ```typescript
34
+ import { Redis } from "@upstash/redis/with-fetch";
35
+ ```
36
+
26
37
  ## Upgrading from v0.2.0?
27
38
 
28
39
  Please read the
@@ -76,6 +87,15 @@ const redis = new Redis({
76
87
  const redis = Redis.fromEnv()
77
88
  ```
78
89
 
90
+ If you are running on nodejs v17 and earlier, `fetch` will not be natively
91
+ supported. Platforms like Vercel, Netlify, Deno, Fastly etc. provide a polyfill
92
+ for you. But if you are running on bare node, you need to either specify a
93
+ polyfill yourself or change the import path to:
94
+
95
+ ```typescript
96
+ import { Redis } from "@upstash/redis/with-fetch";
97
+ ```
98
+
79
99
  - [Code example](https://github.com/upstash/upstash-redis/blob/main/examples/nodejs)
80
100
 
81
101
  #### Cloudflare Workers
@@ -336,3 +356,6 @@ the url and token
336
356
  ```sh
337
357
  UPSTASH_REDIS_REST_URL=".." UPSTASH_REDIS_REST_TOKEN=".." deno test -A
338
358
  ```
359
+
360
+ ```
361
+ ```
@@ -16,6 +16,16 @@ export class Redis extends core.Redis {
16
16
  * ```
17
17
  */
18
18
  constructor(config) {
19
+ if (config.url.startsWith(" ") ||
20
+ config.url.endsWith(" ") ||
21
+ /\r|\n/.test(config.url)) {
22
+ console.warn("The redis url contains whitespace or newline, which can cause errors!");
23
+ }
24
+ if (config.token.startsWith(" ") ||
25
+ config.token.endsWith(" ") ||
26
+ /\r|\n/.test(config.token)) {
27
+ console.warn("The redis token contains whitespace or newline, which can cause errors!");
28
+ }
19
29
  const client = defaultRequester({
20
30
  baseUrl: config.url,
21
31
  headers: { authorization: `Bearer ${config.token}` },
@@ -17,6 +17,16 @@ export class Redis extends core.Redis {
17
17
  * ```
18
18
  */
19
19
  constructor(config) {
20
+ if (config.url.startsWith(" ") ||
21
+ config.url.endsWith(" ") ||
22
+ /\r|\n/.test(config.url)) {
23
+ console.warn("The redis url contains whitespace or newline, which can cause errors!");
24
+ }
25
+ if (config.token.startsWith(" ") ||
26
+ config.token.endsWith(" ") ||
27
+ /\r|\n/.test(config.token)) {
28
+ console.warn("The redis token contains whitespace or newline, which can cause errors!");
29
+ }
20
30
  const client = defaultRequester({
21
31
  baseUrl: config.url,
22
32
  headers: { authorization: `Bearer ${config.token}` },
@@ -0,0 +1,81 @@
1
+ // deno-lint-ignore-file
2
+ import * as core from "../pkg/redis.js";
3
+ import { UpstashError } from "../pkg/error.js";
4
+ import "isomorphic-fetch";
5
+ /**
6
+ * Serverless redis client for upstash.
7
+ */
8
+ export class Redis extends core.Redis {
9
+ constructor(configOrRequester) {
10
+ if ("request" in configOrRequester) {
11
+ super(configOrRequester);
12
+ return;
13
+ }
14
+ if (configOrRequester.url.startsWith(" ") ||
15
+ configOrRequester.url.endsWith(" ") ||
16
+ /\r|\n/.test(configOrRequester.url)) {
17
+ console.warn("The redis url contains whitespace or newline, which can cause errors!");
18
+ }
19
+ if (configOrRequester.token.startsWith(" ") ||
20
+ configOrRequester.token.endsWith(" ") ||
21
+ /\r|\n/.test(configOrRequester.token)) {
22
+ console.warn("The redis token contains whitespace or newline, which can cause errors!");
23
+ }
24
+ const client = defaultRequester({
25
+ baseUrl: configOrRequester.url,
26
+ headers: { authorization: `Bearer ${configOrRequester.token}` },
27
+ // agent: configOrRequester.agent,
28
+ });
29
+ super(client, {
30
+ automaticDeserialization: configOrRequester.automaticDeserialization,
31
+ });
32
+ }
33
+ /**
34
+ * Create a new Upstash Redis instance from environment variables.
35
+ *
36
+ * Use this to automatically load connection secrets from your environment
37
+ * variables. For instance when using the Vercel integration.
38
+ *
39
+ * This tries to load `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` from
40
+ * your environment using `process.env`.
41
+ */
42
+ static fromEnv(config) {
43
+ // @ts-ignore process will be defined in node
44
+ if (typeof process?.env === "undefined") {
45
+ 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');
46
+ }
47
+ // @ts-ignore process will be defined in node
48
+ const url = process?.env["UPSTASH_REDIS_REST_URL"];
49
+ if (!url) {
50
+ throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
51
+ }
52
+ // @ts-ignore process will be defined in node
53
+ const token = process?.env["UPSTASH_REDIS_REST_TOKEN"];
54
+ if (!token) {
55
+ throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
56
+ }
57
+ return new Redis({ url, token, ...config });
58
+ }
59
+ }
60
+ function defaultRequester(config) {
61
+ return {
62
+ request: async function (req) {
63
+ if (!req.path) {
64
+ req.path = [];
65
+ }
66
+ const res = await fetch([config.baseUrl, ...req.path].join("/"), {
67
+ method: "POST",
68
+ headers: { "Content-Type": "application/json", ...config.headers },
69
+ body: JSON.stringify(req.body),
70
+ keepalive: true,
71
+ // @ts-ignore
72
+ agent: config.agent,
73
+ });
74
+ const body = (await res.json());
75
+ if (!res.ok) {
76
+ throw new UpstashError(body.error);
77
+ }
78
+ return body;
79
+ },
80
+ };
81
+ }
@@ -1,11 +1,6 @@
1
1
  // deno-lint-ignore-file
2
2
  import * as core from "../pkg/redis.js";
3
3
  import { UpstashError } from "../pkg/error.js";
4
- // @ts-ignore Deno can't compile
5
- // import https from "https";
6
- // @ts-ignore Deno can't compile
7
- // import http from "http";
8
- import "isomorphic-fetch";
9
4
  /**
10
5
  * Serverless redis client for upstash.
11
6
  */
@@ -15,6 +10,16 @@ export class Redis extends core.Redis {
15
10
  super(configOrRequester);
16
11
  return;
17
12
  }
13
+ if (configOrRequester.url.startsWith(" ") ||
14
+ configOrRequester.url.endsWith(" ") ||
15
+ /\r|\n/.test(configOrRequester.url)) {
16
+ console.warn("The redis url contains whitespace or newline, which can cause errors!");
17
+ }
18
+ if (configOrRequester.token.startsWith(" ") ||
19
+ configOrRequester.token.endsWith(" ") ||
20
+ /\r|\n/.test(configOrRequester.token)) {
21
+ console.warn("The redis token contains whitespace or newline, which can cause errors!");
22
+ }
18
23
  const client = defaultRequester({
19
24
  baseUrl: configOrRequester.url,
20
25
  headers: { authorization: `Bearer ${configOrRequester.token}` },
package/package.json CHANGED
@@ -3,7 +3,6 @@
3
3
  "main": "./script/platforms/nodejs.js",
4
4
  "types": "./types/platforms/nodejs.d.ts",
5
5
  "name": "@upstash/redis",
6
- "version": "v1.3.4",
7
6
  "description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
8
7
  "repository": {
9
8
  "type": "git",
@@ -21,16 +20,15 @@
21
20
  "bugs": {
22
21
  "url": "https://github.com/upstash/upstash-redis/issues"
23
22
  },
23
+ "dependencies": {
24
+ "isomorphic-fetch": "^3.0.0"
25
+ },
24
26
  "homepage": "https://github.com/upstash/upstash-redis#readme",
25
27
  "browser": {
26
28
  "isomorphic-fetch": false,
27
29
  "http": false,
28
30
  "https": false
29
31
  },
30
- "dependencies": {
31
- "encoding": "latest",
32
- "isomorphic-fetch": "^3.0.0"
33
- },
34
32
  "devDependencies": {
35
33
  "@size-limit/preset-small-lib": "latest",
36
34
  "size-limit": "latest"
@@ -81,6 +79,12 @@
81
79
  "import": "./esm/platforms/fastly.js",
82
80
  "require": "./script/platforms/fastly.js",
83
81
  "types": "./types/platforms/fastly.d.ts"
82
+ },
83
+ "./with-fetch": {
84
+ "import": "./esm/platforms/node_with_fetch.js",
85
+ "require": "./script/platforms/node_with_fetch.js",
86
+ "types": "./types/platforms/node_with_fetch.d.ts"
84
87
  }
85
- }
88
+ },
89
+ "version": "1.4.0-next.2"
86
90
  }
@@ -42,6 +42,16 @@ class Redis extends core.Redis {
42
42
  * ```
43
43
  */
44
44
  constructor(config) {
45
+ if (config.url.startsWith(" ") ||
46
+ config.url.endsWith(" ") ||
47
+ /\r|\n/.test(config.url)) {
48
+ console.warn("The redis url contains whitespace or newline, which can cause errors!");
49
+ }
50
+ if (config.token.startsWith(" ") ||
51
+ config.token.endsWith(" ") ||
52
+ /\r|\n/.test(config.token)) {
53
+ console.warn("The redis token contains whitespace or newline, which can cause errors!");
54
+ }
45
55
  const client = defaultRequester({
46
56
  baseUrl: config.url,
47
57
  headers: { authorization: `Bearer ${config.token}` },
@@ -43,6 +43,16 @@ class Redis extends core.Redis {
43
43
  * ```
44
44
  */
45
45
  constructor(config) {
46
+ if (config.url.startsWith(" ") ||
47
+ config.url.endsWith(" ") ||
48
+ /\r|\n/.test(config.url)) {
49
+ console.warn("The redis url contains whitespace or newline, which can cause errors!");
50
+ }
51
+ if (config.token.startsWith(" ") ||
52
+ config.token.endsWith(" ") ||
53
+ /\r|\n/.test(config.token)) {
54
+ console.warn("The redis token contains whitespace or newline, which can cause errors!");
55
+ }
46
56
  const client = defaultRequester({
47
57
  baseUrl: config.url,
48
58
  headers: { authorization: `Bearer ${config.token}` },
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ // deno-lint-ignore-file
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.Redis = void 0;
28
+ const core = __importStar(require("../pkg/redis.js"));
29
+ const error_js_1 = require("../pkg/error.js");
30
+ require("isomorphic-fetch");
31
+ /**
32
+ * Serverless redis client for upstash.
33
+ */
34
+ class Redis extends core.Redis {
35
+ constructor(configOrRequester) {
36
+ if ("request" in configOrRequester) {
37
+ super(configOrRequester);
38
+ return;
39
+ }
40
+ if (configOrRequester.url.startsWith(" ") ||
41
+ configOrRequester.url.endsWith(" ") ||
42
+ /\r|\n/.test(configOrRequester.url)) {
43
+ console.warn("The redis url contains whitespace or newline, which can cause errors!");
44
+ }
45
+ if (configOrRequester.token.startsWith(" ") ||
46
+ configOrRequester.token.endsWith(" ") ||
47
+ /\r|\n/.test(configOrRequester.token)) {
48
+ console.warn("The redis token contains whitespace or newline, which can cause errors!");
49
+ }
50
+ const client = defaultRequester({
51
+ baseUrl: configOrRequester.url,
52
+ headers: { authorization: `Bearer ${configOrRequester.token}` },
53
+ // agent: configOrRequester.agent,
54
+ });
55
+ super(client, {
56
+ automaticDeserialization: configOrRequester.automaticDeserialization,
57
+ });
58
+ }
59
+ /**
60
+ * Create a new Upstash Redis instance from environment variables.
61
+ *
62
+ * Use this to automatically load connection secrets from your environment
63
+ * variables. For instance when using the Vercel integration.
64
+ *
65
+ * This tries to load `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` from
66
+ * your environment using `process.env`.
67
+ */
68
+ static fromEnv(config) {
69
+ // @ts-ignore process will be defined in node
70
+ if (typeof process?.env === "undefined") {
71
+ 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');
72
+ }
73
+ // @ts-ignore process will be defined in node
74
+ const url = process?.env["UPSTASH_REDIS_REST_URL"];
75
+ if (!url) {
76
+ throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
77
+ }
78
+ // @ts-ignore process will be defined in node
79
+ const token = process?.env["UPSTASH_REDIS_REST_TOKEN"];
80
+ if (!token) {
81
+ throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
82
+ }
83
+ return new Redis({ url, token, ...config });
84
+ }
85
+ }
86
+ exports.Redis = Redis;
87
+ function defaultRequester(config) {
88
+ return {
89
+ request: async function (req) {
90
+ if (!req.path) {
91
+ req.path = [];
92
+ }
93
+ const res = await fetch([config.baseUrl, ...req.path].join("/"), {
94
+ method: "POST",
95
+ headers: { "Content-Type": "application/json", ...config.headers },
96
+ body: JSON.stringify(req.body),
97
+ keepalive: true,
98
+ // @ts-ignore
99
+ agent: config.agent,
100
+ });
101
+ const body = (await res.json());
102
+ if (!res.ok) {
103
+ throw new error_js_1.UpstashError(body.error);
104
+ }
105
+ return body;
106
+ },
107
+ };
108
+ }
@@ -27,11 +27,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
27
27
  exports.Redis = void 0;
28
28
  const core = __importStar(require("../pkg/redis.js"));
29
29
  const error_js_1 = require("../pkg/error.js");
30
- // @ts-ignore Deno can't compile
31
- // import https from "https";
32
- // @ts-ignore Deno can't compile
33
- // import http from "http";
34
- require("isomorphic-fetch");
35
30
  /**
36
31
  * Serverless redis client for upstash.
37
32
  */
@@ -41,6 +36,16 @@ class Redis extends core.Redis {
41
36
  super(configOrRequester);
42
37
  return;
43
38
  }
39
+ if (configOrRequester.url.startsWith(" ") ||
40
+ configOrRequester.url.endsWith(" ") ||
41
+ /\r|\n/.test(configOrRequester.url)) {
42
+ console.warn("The redis url contains whitespace or newline, which can cause errors!");
43
+ }
44
+ if (configOrRequester.token.startsWith(" ") ||
45
+ configOrRequester.token.endsWith(" ") ||
46
+ /\r|\n/.test(configOrRequester.token)) {
47
+ console.warn("The redis token contains whitespace or newline, which can cause errors!");
48
+ }
44
49
  const client = defaultRequester({
45
50
  baseUrl: configOrRequester.url,
46
51
  headers: { authorization: `Bearer ${configOrRequester.token}` },
@@ -0,0 +1,63 @@
1
+ import * as core from "../pkg/redis.js";
2
+ import { Requester, UpstashRequest, UpstashResponse } from "../pkg/http.js";
3
+ import "isomorphic-fetch";
4
+ export type { Requester, UpstashRequest, UpstashResponse };
5
+ /**
6
+ * Connection credentials for upstash redis.
7
+ * Get them from https://console.upstash.com/redis/<uuid>
8
+ */
9
+ export declare type RedisConfigNodejs = {
10
+ /**
11
+ * UPSTASH_REDIS_REST_URL
12
+ */
13
+ url: string;
14
+ /**
15
+ * UPSTASH_REDIS_REST_TOKEN
16
+ */
17
+ token: string;
18
+ } & core.RedisOptions;
19
+ /**
20
+ * Serverless redis client for upstash.
21
+ */
22
+ export declare class Redis extends core.Redis {
23
+ /**
24
+ * Create a new redis client by providing the url and token
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * const redis = new Redis({
29
+ * url: "<UPSTASH_REDIS_REST_URL>",
30
+ * token: "<UPSTASH_REDIS_REST_TOKEN>",
31
+ * });
32
+ * ```
33
+ */
34
+ constructor(config: RedisConfigNodejs);
35
+ /**
36
+ * Create a new redis client by providing a custom `Requester` implementation
37
+ *
38
+ * @example
39
+ * ```ts
40
+ *
41
+ * import { UpstashRequest, Requester, UpstashResponse, Redis } from "@upstash/redis"
42
+ *
43
+ * const requester: Requester = {
44
+ * request: <TResult>(req: UpstashRequest): Promise<UpstashResponse<TResult>> => {
45
+ * // ...
46
+ * }
47
+ * }
48
+ *
49
+ * const redis = new Redis(requester)
50
+ * ```
51
+ */
52
+ constructor(requesters: Requester);
53
+ /**
54
+ * Create a new Upstash Redis instance from environment variables.
55
+ *
56
+ * Use this to automatically load connection secrets from your environment
57
+ * variables. For instance when using the Vercel integration.
58
+ *
59
+ * This tries to load `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` from
60
+ * your environment using `process.env`.
61
+ */
62
+ static fromEnv(config?: Omit<RedisConfigNodejs, "url" | "token">): Redis;
63
+ }
@@ -1,6 +1,5 @@
1
1
  import * as core from "../pkg/redis.js";
2
2
  import { Requester, UpstashRequest, UpstashResponse } from "../pkg/http.js";
3
- import "isomorphic-fetch";
4
3
  export type { Requester, UpstashRequest, UpstashResponse };
5
4
  /**
6
5
  * Connection credentials for upstash redis.