@upstash/redis 1.16.0 → 1.16.1-next.1

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/README.md CHANGED
@@ -81,24 +81,6 @@ data = await redis.spop('animals', 1)
81
81
  console.log(data)
82
82
  ```
83
83
 
84
- ### Upgrading to v1.4.0 **(ReferenceError: fetch is not defined)**
85
-
86
- If you are running on nodejs v17 and earlier, `fetch` will not be natively
87
- supported. Platforms like Vercel, Netlify, Deno, Fastly etc. provide a polyfill
88
- for you. But if you are running on bare node, you need to either specify a
89
- polyfill yourself or change the import path to:
90
-
91
- ```typescript
92
- import { Redis } from "@upstash/redis/with-fetch";
93
- ```
94
-
95
- ### Upgrading from v0.2.0?
96
-
97
- Please read the
98
- [migration guide](https://docs.upstash.com/redis/sdks/javascriptsdk/migration).
99
- For further explanation we wrote a
100
- [blog post](https://blog.upstash.com/upstash-redis-sdk-v1).
101
-
102
84
  ## Docs
103
85
 
104
86
  See [the documentation](https://docs.upstash.com/features/javascriptsdk) for
@@ -118,6 +100,3 @@ the url and token
118
100
  ```sh
119
101
  UPSTASH_REDIS_REST_URL=".." UPSTASH_REDIS_REST_TOKEN=".." deno test -A
120
102
  ```
121
-
122
- ```
123
- ```
package/esm/pkg/http.js CHANGED
@@ -25,13 +25,19 @@ export class HttpClient {
25
25
  writable: true,
26
26
  value: void 0
27
27
  });
28
+ this.options = {
29
+ backend: config.options?.backend,
30
+ agent: config.agent,
31
+ responseEncoding: config.responseEncoding ?? "base64", // default to base64
32
+ };
28
33
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
29
34
  this.headers = {
30
35
  "Content-Type": "application/json",
31
- "Upstash-Encoding": "base64",
32
36
  ...config.headers,
33
37
  };
34
- this.options = { backend: config.options?.backend, agent: config.agent };
38
+ if (this.options.responseEncoding === "base64") {
39
+ this.headers["Upstash-Encoding"] = "base64";
40
+ }
35
41
  if (typeof config?.retry === "boolean" && config?.retry === false) {
36
42
  this.retry = {
37
43
  attempts: 1,
@@ -77,23 +83,33 @@ export class HttpClient {
77
83
  if (!res.ok) {
78
84
  throw new UpstashError(body.error);
79
85
  }
80
- return Array.isArray(body) ? body.map(decode) : decode(body);
86
+ if (this.options?.responseEncoding === "base64") {
87
+ return Array.isArray(body) ? body.map(decode) : decode(body);
88
+ }
89
+ return body;
81
90
  }
82
91
  }
83
92
  function base64decode(b64) {
84
93
  let dec = "";
85
94
  try {
86
- dec = atob(b64).split("").map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2)).join("");
95
+ /**
96
+ * Using only atob() is not enough because it doesn't work with unicode characters
97
+ */
98
+ const binString = atob(b64);
99
+ const size = binString.length;
100
+ const bytes = new Uint8Array(size);
101
+ for (let i = 0; i < size; i++) {
102
+ bytes[i] = binString.charCodeAt(i);
103
+ }
104
+ dec = new TextDecoder().decode(bytes);
87
105
  }
88
- catch (e) {
89
- console.warn(`Unable to decode base64 [${dec}]: ${e.message}`);
90
- return dec;
106
+ catch {
107
+ dec = b64;
91
108
  }
92
109
  try {
93
110
  return decodeURIComponent(dec);
94
111
  }
95
- catch (e) {
96
- console.warn(`Unable to decode URI [${dec}]: ${e.message}`);
112
+ catch {
97
113
  return dec;
98
114
  }
99
115
  }
@@ -102,10 +118,11 @@ function decode(raw) {
102
118
  switch (typeof raw.result) {
103
119
  case "undefined":
104
120
  return raw;
105
- case "number":
121
+ case "number": {
106
122
  result = raw.result;
107
123
  break;
108
- case "object":
124
+ }
125
+ case "object": {
109
126
  if (Array.isArray(raw.result)) {
110
127
  result = raw.result.map((v) => typeof v === "string"
111
128
  ? base64decode(v)
@@ -119,9 +136,11 @@ function decode(raw) {
119
136
  result = null;
120
137
  }
121
138
  break;
122
- case "string":
139
+ }
140
+ case "string": {
123
141
  result = raw.result === "OK" ? "OK" : base64decode(raw.result);
124
142
  break;
143
+ }
125
144
  default:
126
145
  break;
127
146
  }
@@ -30,6 +30,7 @@ export class Redis extends core.Redis {
30
30
  retry: config.retry,
31
31
  baseUrl: config.url,
32
32
  headers: { authorization: `Bearer ${config.token}` },
33
+ responseEncoding: config.responseEncoding,
33
34
  });
34
35
  super(client, {
35
36
  automaticDeserialization: config.automaticDeserialization,
@@ -32,6 +32,7 @@ export class Redis extends core.Redis {
32
32
  retry: config.retry,
33
33
  headers: { authorization: `Bearer ${config.token}` },
34
34
  options: { backend: config.backend },
35
+ responseEncoding: config.responseEncoding,
35
36
  });
36
37
  super(client, {
37
38
  automaticDeserialization: config.automaticDeserialization,
@@ -25,7 +25,8 @@ export class Redis extends core.Redis {
25
25
  baseUrl: configOrRequester.url,
26
26
  retry: configOrRequester.retry,
27
27
  headers: { authorization: `Bearer ${configOrRequester.token}` },
28
- // agent: configOrRequester.agent,
28
+ // agent: configOrRequester.agent,
29
+ responseEncoding: configOrRequester.responseEncoding,
29
30
  });
30
31
  super(client, {
31
32
  automaticDeserialization: configOrRequester.automaticDeserialization,
@@ -25,6 +25,7 @@ export class Redis extends core.Redis {
25
25
  retry: configOrRequester.retry,
26
26
  headers: { authorization: `Bearer ${configOrRequester.token}` },
27
27
  agent: configOrRequester.agent,
28
+ responseEncoding: configOrRequester.responseEncoding,
28
29
  });
29
30
  super(client, {
30
31
  automaticDeserialization: configOrRequester.automaticDeserialization,
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.16.0",
7
6
  "description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
8
7
  "repository": {
9
8
  "type": "git",
@@ -59,5 +58,6 @@
59
58
  "require": "./script/platforms/node_with_fetch.js",
60
59
  "types": "./types/platforms/node_with_fetch.d.ts"
61
60
  }
62
- }
61
+ },
62
+ "version": "1.16.1-next.1"
63
63
  }
@@ -28,13 +28,19 @@ class HttpClient {
28
28
  writable: true,
29
29
  value: void 0
30
30
  });
31
+ this.options = {
32
+ backend: config.options?.backend,
33
+ agent: config.agent,
34
+ responseEncoding: config.responseEncoding ?? "base64", // default to base64
35
+ };
31
36
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
32
37
  this.headers = {
33
38
  "Content-Type": "application/json",
34
- "Upstash-Encoding": "base64",
35
39
  ...config.headers,
36
40
  };
37
- this.options = { backend: config.options?.backend, agent: config.agent };
41
+ if (this.options.responseEncoding === "base64") {
42
+ this.headers["Upstash-Encoding"] = "base64";
43
+ }
38
44
  if (typeof config?.retry === "boolean" && config?.retry === false) {
39
45
  this.retry = {
40
46
  attempts: 1,
@@ -80,24 +86,34 @@ class HttpClient {
80
86
  if (!res.ok) {
81
87
  throw new error_js_1.UpstashError(body.error);
82
88
  }
83
- return Array.isArray(body) ? body.map(decode) : decode(body);
89
+ if (this.options?.responseEncoding === "base64") {
90
+ return Array.isArray(body) ? body.map(decode) : decode(body);
91
+ }
92
+ return body;
84
93
  }
85
94
  }
86
95
  exports.HttpClient = HttpClient;
87
96
  function base64decode(b64) {
88
97
  let dec = "";
89
98
  try {
90
- dec = atob(b64).split("").map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2)).join("");
99
+ /**
100
+ * Using only atob() is not enough because it doesn't work with unicode characters
101
+ */
102
+ const binString = atob(b64);
103
+ const size = binString.length;
104
+ const bytes = new Uint8Array(size);
105
+ for (let i = 0; i < size; i++) {
106
+ bytes[i] = binString.charCodeAt(i);
107
+ }
108
+ dec = new TextDecoder().decode(bytes);
91
109
  }
92
- catch (e) {
93
- console.warn(`Unable to decode base64 [${dec}]: ${e.message}`);
94
- return dec;
110
+ catch {
111
+ dec = b64;
95
112
  }
96
113
  try {
97
114
  return decodeURIComponent(dec);
98
115
  }
99
- catch (e) {
100
- console.warn(`Unable to decode URI [${dec}]: ${e.message}`);
116
+ catch {
101
117
  return dec;
102
118
  }
103
119
  }
@@ -106,10 +122,11 @@ function decode(raw) {
106
122
  switch (typeof raw.result) {
107
123
  case "undefined":
108
124
  return raw;
109
- case "number":
125
+ case "number": {
110
126
  result = raw.result;
111
127
  break;
112
- case "object":
128
+ }
129
+ case "object": {
113
130
  if (Array.isArray(raw.result)) {
114
131
  result = raw.result.map((v) => typeof v === "string"
115
132
  ? base64decode(v)
@@ -123,9 +140,11 @@ function decode(raw) {
123
140
  result = null;
124
141
  }
125
142
  break;
126
- case "string":
143
+ }
144
+ case "string": {
127
145
  result = raw.result === "OK" ? "OK" : base64decode(raw.result);
128
146
  break;
147
+ }
129
148
  default:
130
149
  break;
131
150
  }
@@ -56,6 +56,7 @@ class Redis extends core.Redis {
56
56
  retry: config.retry,
57
57
  baseUrl: config.url,
58
58
  headers: { authorization: `Bearer ${config.token}` },
59
+ responseEncoding: config.responseEncoding,
59
60
  });
60
61
  super(client, {
61
62
  automaticDeserialization: config.automaticDeserialization,
@@ -58,6 +58,7 @@ class Redis extends core.Redis {
58
58
  retry: config.retry,
59
59
  headers: { authorization: `Bearer ${config.token}` },
60
60
  options: { backend: config.backend },
61
+ responseEncoding: config.responseEncoding,
61
62
  });
62
63
  super(client, {
63
64
  automaticDeserialization: config.automaticDeserialization,
@@ -51,7 +51,8 @@ class Redis extends core.Redis {
51
51
  baseUrl: configOrRequester.url,
52
52
  retry: configOrRequester.retry,
53
53
  headers: { authorization: `Bearer ${configOrRequester.token}` },
54
- // agent: configOrRequester.agent,
54
+ // agent: configOrRequester.agent,
55
+ responseEncoding: configOrRequester.responseEncoding,
55
56
  });
56
57
  super(client, {
57
58
  automaticDeserialization: configOrRequester.automaticDeserialization,
@@ -51,6 +51,7 @@ class Redis extends core.Redis {
51
51
  retry: configOrRequester.retry,
52
52
  headers: { authorization: `Bearer ${configOrRequester.token}` },
53
53
  agent: configOrRequester.agent,
54
+ responseEncoding: configOrRequester.responseEncoding,
54
55
  });
55
56
  super(client, {
56
57
  automaticDeserialization: configOrRequester.automaticDeserialization,
@@ -29,22 +29,52 @@ export declare type RetryConfig = false | {
29
29
  */
30
30
  backoff?: (retryCount: number) => number;
31
31
  };
32
- declare type Options = {
32
+ export declare type Options = {
33
33
  backend?: string;
34
34
  };
35
+ export declare type RequesterConfig = {
36
+ /**
37
+ * Configure the retry behaviour in case of network errors
38
+ */
39
+ retry?: RetryConfig;
40
+ /**
41
+ * Due to the nature of dynamic and custom data, it is possible to write data to redis that is not
42
+ * valid json and will therefore cause errors when deserializing. This used to happen very
43
+ * frequently with non-utf8 data, such as emojis.
44
+ *
45
+ * By default we will therefore encode the data as base64 on the server, before sending it to the
46
+ * client. The client will then decode the base64 data and parse it as utf8.
47
+ *
48
+ * For very large entries, this can add a few milliseconds, so if you are sure that your data is
49
+ * valid utf8, you can disable this behaviour by setting this option to false.
50
+ *
51
+ * Here's what the response body looks like:
52
+ *
53
+ * ```json
54
+ * {
55
+ * result?: "base64-encoded",
56
+ * error?: string
57
+ * }
58
+ * ```
59
+ *
60
+ * @default "base64"
61
+ */
62
+ responseEncoding?: false | "base64";
63
+ };
35
64
  export declare type HttpClientConfig = {
36
65
  headers?: Record<string, string>;
37
66
  baseUrl: string;
38
67
  options?: Options;
39
68
  retry?: RetryConfig;
40
69
  agent?: any;
41
- };
70
+ } & RequesterConfig;
42
71
  export declare class HttpClient implements Requester {
43
72
  baseUrl: string;
44
73
  headers: Record<string, string>;
45
- readonly options?: {
74
+ readonly options: {
46
75
  backend?: string;
47
76
  agent: any;
77
+ responseEncoding?: false | "base64";
48
78
  };
49
79
  readonly retry: {
50
80
  attempts: number;
@@ -53,4 +83,3 @@ export declare class HttpClient implements Requester {
53
83
  constructor(config: HttpClientConfig);
54
84
  request<TResult>(req: UpstashRequest): Promise<UpstashResponse<TResult>>;
55
85
  }
56
- export {};
@@ -1,5 +1,6 @@
1
1
  import * as core from "../pkg/redis.js";
2
- import type { Requester, RetryConfig, UpstashRequest, UpstashResponse } from "../pkg/http.js";
2
+ import type { Requester, UpstashRequest, UpstashResponse } from "../pkg/http.js";
3
+ import { RequesterConfig } from "../pkg/http.js";
3
4
  export type { Requester, UpstashRequest, UpstashResponse };
4
5
  /**
5
6
  * Connection credentials for upstash redis.
@@ -14,11 +15,7 @@ export declare type RedisConfigCloudflare = {
14
15
  * UPSTASH_REDIS_REST_TOKEN
15
16
  */
16
17
  token: string;
17
- /**
18
- * Configure the retry behaviour in case of network errors
19
- */
20
- retry?: RetryConfig;
21
- } & core.RedisOptions;
18
+ } & core.RedisOptions & RequesterConfig;
22
19
  /**
23
20
  * Serverless redis client for upstash.
24
21
  */
@@ -1,5 +1,5 @@
1
1
  import * as core from "../pkg/redis.js";
2
- import type { Requester, RetryConfig, UpstashRequest, UpstashResponse } from "../pkg/http.js";
2
+ import type { Requester, RequesterConfig, UpstashRequest, UpstashResponse } from "../pkg/http.js";
3
3
  export type { Requester, UpstashRequest, UpstashResponse };
4
4
  /**
5
5
  * Connection credentials for upstash redis.
@@ -20,11 +20,7 @@ export declare type RedisConfigFastly = {
20
20
  * referenced by name.
21
21
  */
22
22
  backend: string;
23
- /**
24
- * Configure the retry behaviour in case of network errors
25
- */
26
- retry?: RetryConfig;
27
- } & core.RedisOptions;
23
+ } & core.RedisOptions & RequesterConfig;
28
24
  /**
29
25
  * Serverless redis client for upstash.
30
26
  */
@@ -1,5 +1,5 @@
1
1
  import * as core from "../pkg/redis.js";
2
- import { Requester, RetryConfig, UpstashRequest, UpstashResponse } from "../pkg/http.js";
2
+ import { Requester, RequesterConfig, UpstashRequest, UpstashResponse } from "../pkg/http.js";
3
3
  import "isomorphic-fetch";
4
4
  export type { Requester, UpstashRequest, UpstashResponse };
5
5
  /**
@@ -15,26 +15,7 @@ export declare type RedisConfigNodejs = {
15
15
  * UPSTASH_REDIS_REST_TOKEN
16
16
  */
17
17
  token: string;
18
- /**
19
- * An agent allows you to reuse connections to reduce latency for multiple sequential requests.
20
- *
21
- * This is a node specific implementation and is not supported in various runtimes like Vercel
22
- * edge functions.
23
- *
24
- * @example
25
- * ```ts
26
- * import https from "https"
27
- *
28
- * const options: RedisConfigNodejs = {
29
- * agent: new https.Agent({ keepAlive: true })
30
- * }
31
- * ```
32
- */
33
- /**
34
- * Configure the retry behaviour in case of network errors
35
- */
36
- retry?: RetryConfig;
37
- } & core.RedisOptions;
18
+ } & core.RedisOptions & RequesterConfig;
38
19
  /**
39
20
  * Serverless redis client for upstash.
40
21
  */
@@ -1,5 +1,5 @@
1
1
  import * as core from "../pkg/redis.js";
2
- import { Requester, RetryConfig, UpstashRequest, UpstashResponse } from "../pkg/http.js";
2
+ import { Requester, RequesterConfig, UpstashRequest, UpstashResponse } from "../pkg/http.js";
3
3
  export type { Requester, UpstashRequest, UpstashResponse };
4
4
  /**
5
5
  * Connection credentials for upstash redis.
@@ -30,11 +30,7 @@ export declare type RedisConfigNodejs = {
30
30
  * ```
31
31
  */
32
32
  agent?: any;
33
- /**
34
- * Configure the retry behaviour in case of network errors
35
- */
36
- retry?: RetryConfig;
37
- } & core.RedisOptions;
33
+ } & core.RedisOptions & RequesterConfig;
38
34
  /**
39
35
  * Serverless redis client for upstash.
40
36
  */