@upstash/redis 1.16.0 → 1.16.1-rc.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 +0 -21
- package/esm/pkg/http.js +31 -9
- package/esm/platforms/cloudflare.js +1 -0
- package/esm/platforms/fastly.js +1 -0
- package/esm/platforms/node_with_fetch.js +2 -1
- package/esm/platforms/nodejs.js +1 -0
- package/package.json +1 -1
- package/script/pkg/http.js +31 -9
- package/script/platforms/cloudflare.js +1 -0
- package/script/platforms/fastly.js +1 -0
- package/script/platforms/node_with_fetch.js +2 -1
- package/script/platforms/nodejs.js +1 -0
- package/types/pkg/http.d.ts +32 -3
- package/types/platforms/cloudflare.d.ts +3 -6
- package/types/platforms/fastly.d.ts +2 -6
- package/types/platforms/node_with_fetch.d.ts +2 -21
- package/types/platforms/nodejs.d.ts +2 -6
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
|
|
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,36 @@ export class HttpClient {
|
|
|
77
83
|
if (!res.ok) {
|
|
78
84
|
throw new UpstashError(body.error);
|
|
79
85
|
}
|
|
80
|
-
|
|
86
|
+
if (this.options?.responseEncoding === "base64") {
|
|
87
|
+
console.log("decoding base64");
|
|
88
|
+
return Array.isArray(body) ? body.map(decode) : decode(body);
|
|
89
|
+
}
|
|
90
|
+
return body;
|
|
81
91
|
}
|
|
82
92
|
}
|
|
83
93
|
function base64decode(b64) {
|
|
84
94
|
let dec = "";
|
|
85
95
|
try {
|
|
86
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Using only atob() is not enough because it doesn't work with unicode characters
|
|
98
|
+
*/
|
|
99
|
+
const binString = atob(b64);
|
|
100
|
+
const size = binString.length;
|
|
101
|
+
const bytes = new Uint8Array(size);
|
|
102
|
+
for (let i = 0; i < size; i++) {
|
|
103
|
+
bytes[i] = binString.charCodeAt(i);
|
|
104
|
+
}
|
|
105
|
+
dec = new TextDecoder().decode(bytes);
|
|
87
106
|
}
|
|
88
107
|
catch (e) {
|
|
89
108
|
console.warn(`Unable to decode base64 [${dec}]: ${e.message}`);
|
|
90
|
-
return
|
|
109
|
+
return b64;
|
|
91
110
|
}
|
|
92
111
|
try {
|
|
93
112
|
return decodeURIComponent(dec);
|
|
94
113
|
}
|
|
95
114
|
catch (e) {
|
|
96
|
-
console.warn(`Unable to decode URI
|
|
115
|
+
console.warn(`Unable to decode URI[${dec}]: ${e.message}`);
|
|
97
116
|
return dec;
|
|
98
117
|
}
|
|
99
118
|
}
|
|
@@ -102,10 +121,11 @@ function decode(raw) {
|
|
|
102
121
|
switch (typeof raw.result) {
|
|
103
122
|
case "undefined":
|
|
104
123
|
return raw;
|
|
105
|
-
case "number":
|
|
124
|
+
case "number": {
|
|
106
125
|
result = raw.result;
|
|
107
126
|
break;
|
|
108
|
-
|
|
127
|
+
}
|
|
128
|
+
case "object": {
|
|
109
129
|
if (Array.isArray(raw.result)) {
|
|
110
130
|
result = raw.result.map((v) => typeof v === "string"
|
|
111
131
|
? base64decode(v)
|
|
@@ -119,9 +139,11 @@ function decode(raw) {
|
|
|
119
139
|
result = null;
|
|
120
140
|
}
|
|
121
141
|
break;
|
|
122
|
-
|
|
142
|
+
}
|
|
143
|
+
case "string": {
|
|
123
144
|
result = raw.result === "OK" ? "OK" : base64decode(raw.result);
|
|
124
145
|
break;
|
|
146
|
+
}
|
|
125
147
|
default:
|
|
126
148
|
break;
|
|
127
149
|
}
|
|
@@ -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,
|
package/esm/platforms/fastly.js
CHANGED
|
@@ -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
|
-
//
|
|
28
|
+
// agent: configOrRequester.agent,
|
|
29
|
+
responseEncoding: configOrRequester.responseEncoding,
|
|
29
30
|
});
|
|
30
31
|
super(client, {
|
|
31
32
|
automaticDeserialization: configOrRequester.automaticDeserialization,
|
package/esm/platforms/nodejs.js
CHANGED
|
@@ -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,7 @@
|
|
|
3
3
|
"main": "./script/platforms/nodejs.js",
|
|
4
4
|
"types": "./types/platforms/nodejs.d.ts",
|
|
5
5
|
"name": "@upstash/redis",
|
|
6
|
-
"version": "v1.16.
|
|
6
|
+
"version": "v1.16.1-rc.1",
|
|
7
7
|
"description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
package/script/pkg/http.js
CHANGED
|
@@ -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
|
|
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,37 @@ class HttpClient {
|
|
|
80
86
|
if (!res.ok) {
|
|
81
87
|
throw new error_js_1.UpstashError(body.error);
|
|
82
88
|
}
|
|
83
|
-
|
|
89
|
+
if (this.options?.responseEncoding === "base64") {
|
|
90
|
+
console.log("decoding base64");
|
|
91
|
+
return Array.isArray(body) ? body.map(decode) : decode(body);
|
|
92
|
+
}
|
|
93
|
+
return body;
|
|
84
94
|
}
|
|
85
95
|
}
|
|
86
96
|
exports.HttpClient = HttpClient;
|
|
87
97
|
function base64decode(b64) {
|
|
88
98
|
let dec = "";
|
|
89
99
|
try {
|
|
90
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Using only atob() is not enough because it doesn't work with unicode characters
|
|
102
|
+
*/
|
|
103
|
+
const binString = atob(b64);
|
|
104
|
+
const size = binString.length;
|
|
105
|
+
const bytes = new Uint8Array(size);
|
|
106
|
+
for (let i = 0; i < size; i++) {
|
|
107
|
+
bytes[i] = binString.charCodeAt(i);
|
|
108
|
+
}
|
|
109
|
+
dec = new TextDecoder().decode(bytes);
|
|
91
110
|
}
|
|
92
111
|
catch (e) {
|
|
93
112
|
console.warn(`Unable to decode base64 [${dec}]: ${e.message}`);
|
|
94
|
-
return
|
|
113
|
+
return b64;
|
|
95
114
|
}
|
|
96
115
|
try {
|
|
97
116
|
return decodeURIComponent(dec);
|
|
98
117
|
}
|
|
99
118
|
catch (e) {
|
|
100
|
-
console.warn(`Unable to decode URI
|
|
119
|
+
console.warn(`Unable to decode URI[${dec}]: ${e.message}`);
|
|
101
120
|
return dec;
|
|
102
121
|
}
|
|
103
122
|
}
|
|
@@ -106,10 +125,11 @@ function decode(raw) {
|
|
|
106
125
|
switch (typeof raw.result) {
|
|
107
126
|
case "undefined":
|
|
108
127
|
return raw;
|
|
109
|
-
case "number":
|
|
128
|
+
case "number": {
|
|
110
129
|
result = raw.result;
|
|
111
130
|
break;
|
|
112
|
-
|
|
131
|
+
}
|
|
132
|
+
case "object": {
|
|
113
133
|
if (Array.isArray(raw.result)) {
|
|
114
134
|
result = raw.result.map((v) => typeof v === "string"
|
|
115
135
|
? base64decode(v)
|
|
@@ -123,9 +143,11 @@ function decode(raw) {
|
|
|
123
143
|
result = null;
|
|
124
144
|
}
|
|
125
145
|
break;
|
|
126
|
-
|
|
146
|
+
}
|
|
147
|
+
case "string": {
|
|
127
148
|
result = raw.result === "OK" ? "OK" : base64decode(raw.result);
|
|
128
149
|
break;
|
|
150
|
+
}
|
|
129
151
|
default:
|
|
130
152
|
break;
|
|
131
153
|
}
|
|
@@ -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
|
-
//
|
|
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,
|
package/types/pkg/http.d.ts
CHANGED
|
@@ -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
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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
|
*/
|