@upstash/redis 1.13.1 → 1.14.0

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/esm/pkg/http.js CHANGED
@@ -26,7 +26,11 @@ export class HttpClient {
26
26
  value: void 0
27
27
  });
28
28
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
29
- this.headers = { "Content-Type": "application/json", ...config.headers };
29
+ this.headers = {
30
+ "Content-Type": "application/json",
31
+ "Upstash-Encoding": "base64",
32
+ ...config.headers,
33
+ };
30
34
  this.options = { backend: config.options?.backend };
31
35
  if (typeof config?.retry === "boolean" && config?.retry === false) {
32
36
  this.retry = {
@@ -72,6 +76,53 @@ export class HttpClient {
72
76
  if (!res.ok) {
73
77
  throw new UpstashError(body.error);
74
78
  }
75
- return body;
79
+ return Array.isArray(body) ? body.map(decode) : decode(body);
80
+ }
81
+ }
82
+ function base64decode(b64) {
83
+ let dec = "";
84
+ try {
85
+ dec = atob(b64).split("").map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2)).join("");
86
+ }
87
+ catch (e) {
88
+ console.warn(`Unable to decode base64 [${dec}]: ${e.message}`);
89
+ return dec;
90
+ }
91
+ try {
92
+ return decodeURIComponent(dec);
93
+ }
94
+ catch (e) {
95
+ console.warn(`Unable to decode URI [${dec}]: ${e.message}`);
96
+ return dec;
97
+ }
98
+ }
99
+ function decode(raw) {
100
+ let result = undefined;
101
+ switch (typeof raw.result) {
102
+ case "undefined":
103
+ return raw;
104
+ case "number":
105
+ result = raw.result;
106
+ break;
107
+ case "object":
108
+ if (Array.isArray(raw.result)) {
109
+ result = raw.result.map((v) => typeof v === "string"
110
+ ? base64decode(v)
111
+ : Array.isArray(v)
112
+ ? v.map(base64decode)
113
+ : v);
114
+ }
115
+ else {
116
+ // If it's not an array it must be null
117
+ // Apparently null is an object in javascript
118
+ result = null;
119
+ }
120
+ break;
121
+ case "string":
122
+ result = raw.result === "OK" ? "OK" : base64decode(raw.result);
123
+ break;
124
+ default:
125
+ break;
76
126
  }
127
+ return { result, error: raw.error };
77
128
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "./script/platforms/nodejs.js",
4
4
  "types": "./types/platforms/nodejs.d.ts",
5
5
  "name": "@upstash/redis",
6
- "version": "v1.13.1",
6
+ "version": "v1.14.0",
7
7
  "description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
8
8
  "repository": {
9
9
  "type": "git",
@@ -29,7 +29,11 @@ class HttpClient {
29
29
  value: void 0
30
30
  });
31
31
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
32
- this.headers = { "Content-Type": "application/json", ...config.headers };
32
+ this.headers = {
33
+ "Content-Type": "application/json",
34
+ "Upstash-Encoding": "base64",
35
+ ...config.headers,
36
+ };
33
37
  this.options = { backend: config.options?.backend };
34
38
  if (typeof config?.retry === "boolean" && config?.retry === false) {
35
39
  this.retry = {
@@ -75,7 +79,54 @@ class HttpClient {
75
79
  if (!res.ok) {
76
80
  throw new error_js_1.UpstashError(body.error);
77
81
  }
78
- return body;
82
+ return Array.isArray(body) ? body.map(decode) : decode(body);
79
83
  }
80
84
  }
81
85
  exports.HttpClient = HttpClient;
86
+ function base64decode(b64) {
87
+ let dec = "";
88
+ try {
89
+ dec = atob(b64).split("").map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2)).join("");
90
+ }
91
+ catch (e) {
92
+ console.warn(`Unable to decode base64 [${dec}]: ${e.message}`);
93
+ return dec;
94
+ }
95
+ try {
96
+ return decodeURIComponent(dec);
97
+ }
98
+ catch (e) {
99
+ console.warn(`Unable to decode URI [${dec}]: ${e.message}`);
100
+ return dec;
101
+ }
102
+ }
103
+ function decode(raw) {
104
+ let result = undefined;
105
+ switch (typeof raw.result) {
106
+ case "undefined":
107
+ return raw;
108
+ case "number":
109
+ result = raw.result;
110
+ break;
111
+ case "object":
112
+ if (Array.isArray(raw.result)) {
113
+ result = raw.result.map((v) => typeof v === "string"
114
+ ? base64decode(v)
115
+ : Array.isArray(v)
116
+ ? v.map(base64decode)
117
+ : v);
118
+ }
119
+ else {
120
+ // If it's not an array it must be null
121
+ // Apparently null is an object in javascript
122
+ result = null;
123
+ }
124
+ break;
125
+ case "string":
126
+ result = raw.result === "OK" ? "OK" : base64decode(raw.result);
127
+ break;
128
+ default:
129
+ break;
130
+ }
131
+ return { result, error: raw.error };
132
+ }