@upstash/redis 0.0.0-ci.b4d4a1cc → 0.0.0-ci.b8efaf97-20230119
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 +43 -279
- package/esm/deps/deno.land/x/base64@v0.2.1/base.js +100 -0
- package/esm/deps/deno.land/x/base64@v0.2.1/base64url.js +9 -0
- package/esm/deps/deno.land/x/sha1@v1.0.3/deps.js +1 -0
- package/esm/deps/deno.land/x/sha1@v1.0.3/mod.js +191 -0
- package/esm/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +50 -0
- package/esm/pkg/commands/getdel.js +9 -0
- package/esm/pkg/commands/hgetall.js +0 -4
- package/esm/pkg/commands/hrandfield.js +39 -0
- package/esm/pkg/commands/lmove.js +9 -0
- package/esm/pkg/commands/lpos.js +19 -0
- package/esm/pkg/commands/mod.js +5 -0
- package/esm/pkg/commands/scan.js +3 -0
- package/esm/pkg/commands/sdiffstore.js +1 -1
- package/esm/pkg/commands/set.js +16 -4
- package/esm/pkg/commands/smismember.js +9 -0
- package/esm/pkg/commands/zdiffstore.js +9 -0
- package/esm/pkg/commands/zmscore.js +10 -0
- package/esm/pkg/commands/zrange.js +6 -0
- package/esm/pkg/http.js +133 -8
- package/esm/pkg/pipeline.js +80 -6
- package/esm/pkg/redis.js +125 -2
- package/esm/pkg/script.js +77 -0
- package/esm/platforms/cloudflare.js +14 -26
- package/esm/platforms/fastly.js +5 -25
- package/esm/platforms/node_with_fetch.js +31 -26
- package/esm/platforms/nodejs.js +26 -26
- package/esm/version.js +1 -0
- package/package.json +1 -39
- package/script/deps/deno.land/x/base64@v0.2.1/base.js +104 -0
- package/script/deps/deno.land/x/base64@v0.2.1/base64url.js +13 -0
- package/script/deps/deno.land/x/sha1@v1.0.3/deps.js +6 -0
- package/script/deps/deno.land/x/sha1@v1.0.3/mod.js +196 -0
- package/script/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +55 -0
- package/script/pkg/commands/getdel.js +13 -0
- package/script/pkg/commands/hgetall.js +0 -4
- package/script/pkg/commands/hrandfield.js +43 -0
- package/script/pkg/commands/lmove.js +13 -0
- package/script/pkg/commands/lpos.js +23 -0
- package/script/pkg/commands/mod.js +5 -0
- package/script/pkg/commands/scan.js +3 -0
- package/script/pkg/commands/sdiffstore.js +1 -1
- package/script/pkg/commands/set.js +16 -4
- package/script/pkg/commands/smismember.js +13 -0
- package/script/pkg/commands/zdiffstore.js +13 -0
- package/script/pkg/commands/zmscore.js +14 -0
- package/script/pkg/commands/zrange.js +6 -0
- package/script/pkg/http.js +133 -8
- package/script/pkg/pipeline.js +79 -5
- package/script/pkg/redis.js +124 -1
- package/script/pkg/script.js +81 -0
- package/script/platforms/cloudflare.js +14 -26
- package/script/platforms/fastly.js +5 -25
- package/script/platforms/node_with_fetch.js +31 -26
- package/script/platforms/nodejs.js +26 -26
- package/script/version.js +4 -0
- package/types/deps/deno.land/x/base64@v0.2.1/base.d.ts +5 -0
- package/types/deps/deno.land/x/base64@v0.2.1/base64url.d.ts +1 -0
- package/types/deps/deno.land/x/sha1@v1.0.3/deps.d.ts +1 -0
- package/types/deps/deno.land/x/sha1@v1.0.3/mod.d.ts +26 -0
- package/types/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.d.ts +3 -0
- package/types/pkg/commands/bitop.d.ts +0 -1
- package/types/pkg/commands/bitpos.d.ts +1 -1
- package/types/pkg/commands/getdel.d.ts +7 -0
- package/types/pkg/commands/hrandfield.d.ts +9 -0
- package/types/pkg/commands/lmove.d.ts +12 -0
- package/types/pkg/commands/lpop.d.ts +1 -1
- package/types/pkg/commands/lpos.d.ts +15 -0
- package/types/pkg/commands/mget.d.ts +1 -1
- package/types/pkg/commands/mod.d.ts +5 -0
- package/types/pkg/commands/rpop.d.ts +2 -2
- package/types/pkg/commands/scan.d.ts +1 -0
- package/types/pkg/commands/sdiffstore.d.ts +1 -1
- package/types/pkg/commands/set.d.ts +31 -2
- package/types/pkg/commands/smembers.d.ts +2 -2
- package/types/pkg/commands/smismember.d.ts +7 -0
- package/types/pkg/commands/spop.d.ts +2 -2
- package/types/pkg/commands/zadd.d.ts +1 -1
- package/types/pkg/commands/zdiffstore.d.ts +7 -0
- package/types/pkg/commands/zmscore.d.ts +7 -0
- package/types/pkg/commands/zrange.d.ts +7 -0
- package/types/pkg/http.d.ts +63 -5
- package/types/pkg/pipeline.d.ts +46 -9
- package/types/pkg/redis.d.ts +65 -9
- package/types/pkg/script.d.ts +42 -0
- package/types/pkg/types.d.ts +17 -0
- package/types/platforms/cloudflare.d.ts +8 -3
- package/types/platforms/fastly.d.ts +2 -2
- package/types/platforms/node_with_fetch.d.ts +2 -2
- package/types/platforms/nodejs.d.ts +18 -2
- package/types/version.d.ts +1 -0
package/script/pkg/http.js
CHANGED
|
@@ -22,32 +22,157 @@ class HttpClient {
|
|
|
22
22
|
writable: true,
|
|
23
23
|
value: void 0
|
|
24
24
|
});
|
|
25
|
+
Object.defineProperty(this, "retry", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: void 0
|
|
30
|
+
});
|
|
31
|
+
this.options = {
|
|
32
|
+
backend: config.options?.backend,
|
|
33
|
+
agent: config.agent,
|
|
34
|
+
responseEncoding: config.responseEncoding ?? "base64", // default to base64
|
|
35
|
+
};
|
|
25
36
|
this.baseUrl = config.baseUrl.replace(/\/$/, "");
|
|
26
|
-
this.headers = {
|
|
27
|
-
|
|
37
|
+
this.headers = {
|
|
38
|
+
"Content-Type": "application/json",
|
|
39
|
+
...config.headers,
|
|
40
|
+
};
|
|
41
|
+
if (config.telemetry?.runtime) {
|
|
42
|
+
this.headers["Upstash-Telemetry-Runtime"] = config.telemetry.runtime;
|
|
43
|
+
}
|
|
44
|
+
if (config.telemetry?.platform) {
|
|
45
|
+
this.headers["Upstash-Telemetry-Platform"] = config.telemetry.platform;
|
|
46
|
+
}
|
|
47
|
+
if (config.telemetry?.sdk) {
|
|
48
|
+
this.headers["Upstash-Telemetry-Sdk"] = config.telemetry.sdk;
|
|
49
|
+
}
|
|
50
|
+
if (this.options.responseEncoding === "base64") {
|
|
51
|
+
this.headers["Upstash-Encoding"] = "base64";
|
|
52
|
+
}
|
|
53
|
+
if (typeof config?.retry === "boolean" && config?.retry === false) {
|
|
54
|
+
this.retry = {
|
|
55
|
+
attempts: 1,
|
|
56
|
+
backoff: () => 0,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.retry = {
|
|
61
|
+
attempts: config?.retry?.retries ?? 5,
|
|
62
|
+
backoff: config?.retry?.backoff ??
|
|
63
|
+
((retryCount) => Math.exp(retryCount) * 50),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
28
66
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
67
|
+
mergeTelemetry(telemetry) {
|
|
68
|
+
function merge(obj, key, value) {
|
|
69
|
+
if (!value) {
|
|
70
|
+
return obj;
|
|
71
|
+
}
|
|
72
|
+
if (obj[key]) {
|
|
73
|
+
obj[key] = [obj[key], value].join(",");
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
obj[key] = value;
|
|
77
|
+
}
|
|
78
|
+
return obj;
|
|
32
79
|
}
|
|
80
|
+
this.headers = merge(this.headers, "Upstash-Telemetry-Runtime", telemetry.runtime);
|
|
81
|
+
this.headers = merge(this.headers, "Upstash-Telemetry-Platform", telemetry.platform);
|
|
82
|
+
this.headers = merge(this.headers, "Upstash-Telemetry-Sdk", telemetry.sdk);
|
|
83
|
+
}
|
|
84
|
+
async request(req) {
|
|
33
85
|
const requestOptions = {
|
|
34
86
|
method: "POST",
|
|
35
87
|
headers: this.headers,
|
|
36
88
|
body: JSON.stringify(req.body),
|
|
37
89
|
keepalive: true,
|
|
90
|
+
agent: this.options?.agent,
|
|
38
91
|
/**
|
|
39
92
|
* Fastly specific
|
|
40
93
|
*/
|
|
41
94
|
backend: this.options?.backend,
|
|
42
95
|
};
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
96
|
+
let res = null;
|
|
97
|
+
let error = null;
|
|
98
|
+
for (let i = 0; i <= this.retry.attempts; i++) {
|
|
99
|
+
try {
|
|
100
|
+
res = await fetch([this.baseUrl, ...(req.path ?? [])].join("/"), requestOptions);
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
error = err;
|
|
105
|
+
await new Promise((r) => setTimeout(r, this.retry.backoff(i)));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (!res) {
|
|
109
|
+
throw error ?? new Error("Exhausted all retries");
|
|
110
|
+
}
|
|
46
111
|
const body = (await res.json());
|
|
47
112
|
if (!res.ok) {
|
|
48
113
|
throw new error_js_1.UpstashError(body.error);
|
|
49
114
|
}
|
|
115
|
+
if (this.options?.responseEncoding === "base64") {
|
|
116
|
+
return Array.isArray(body) ? body.map(decode) : decode(body);
|
|
117
|
+
}
|
|
50
118
|
return body;
|
|
51
119
|
}
|
|
52
120
|
}
|
|
53
121
|
exports.HttpClient = HttpClient;
|
|
122
|
+
function base64decode(b64) {
|
|
123
|
+
let dec = "";
|
|
124
|
+
try {
|
|
125
|
+
/**
|
|
126
|
+
* Using only atob() is not enough because it doesn't work with unicode characters
|
|
127
|
+
*/
|
|
128
|
+
const binString = atob(b64);
|
|
129
|
+
const size = binString.length;
|
|
130
|
+
const bytes = new Uint8Array(size);
|
|
131
|
+
for (let i = 0; i < size; i++) {
|
|
132
|
+
bytes[i] = binString.charCodeAt(i);
|
|
133
|
+
}
|
|
134
|
+
dec = new TextDecoder().decode(bytes);
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
dec = b64;
|
|
138
|
+
}
|
|
139
|
+
return dec;
|
|
140
|
+
// try {
|
|
141
|
+
// return decodeURIComponent(dec);
|
|
142
|
+
// } catch {
|
|
143
|
+
// return dec;
|
|
144
|
+
// }
|
|
145
|
+
}
|
|
146
|
+
function decode(raw) {
|
|
147
|
+
let result = undefined;
|
|
148
|
+
switch (typeof raw.result) {
|
|
149
|
+
case "undefined":
|
|
150
|
+
return raw;
|
|
151
|
+
case "number": {
|
|
152
|
+
result = raw.result;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
case "object": {
|
|
156
|
+
if (Array.isArray(raw.result)) {
|
|
157
|
+
result = raw.result.map((v) => typeof v === "string"
|
|
158
|
+
? base64decode(v)
|
|
159
|
+
: Array.isArray(v)
|
|
160
|
+
? v.map(base64decode)
|
|
161
|
+
: v);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
// If it's not an array it must be null
|
|
165
|
+
// Apparently null is an object in javascript
|
|
166
|
+
result = null;
|
|
167
|
+
}
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case "string": {
|
|
171
|
+
result = raw.result === "OK" ? "OK" : base64decode(raw.result);
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
default:
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
return { result, error: raw.error };
|
|
178
|
+
}
|
package/script/pkg/pipeline.js
CHANGED
|
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Pipeline = void 0;
|
|
4
4
|
const mod_js_1 = require("./commands/mod.js");
|
|
5
5
|
const error_js_1 = require("./error.js");
|
|
6
|
+
const zmscore_js_1 = require("./commands/zmscore.js");
|
|
7
|
+
const hrandfield_js_1 = require("./commands/hrandfield.js");
|
|
8
|
+
const zdiffstore_js_1 = require("./commands/zdiffstore.js");
|
|
6
9
|
/**
|
|
7
10
|
* Upstash REST API supports command pipelining to send multiple commands in
|
|
8
11
|
* batch, instead of sending each command one by one and waiting for a response.
|
|
@@ -19,7 +22,7 @@ const error_js_1 = require("./error.js");
|
|
|
19
22
|
* **Examples:**
|
|
20
23
|
*
|
|
21
24
|
* ```ts
|
|
22
|
-
* const p = redis.pipeline()
|
|
25
|
+
* const p = redis.pipeline() // or redis.multi()
|
|
23
26
|
* p.set("key","value")
|
|
24
27
|
* p.get("key")
|
|
25
28
|
* const res = await p.exec()
|
|
@@ -42,7 +45,7 @@ const error_js_1 = require("./error.js");
|
|
|
42
45
|
* ```
|
|
43
46
|
*/
|
|
44
47
|
class Pipeline {
|
|
45
|
-
constructor(
|
|
48
|
+
constructor(opts) {
|
|
46
49
|
Object.defineProperty(this, "client", {
|
|
47
50
|
enumerable: true,
|
|
48
51
|
configurable: true,
|
|
@@ -61,6 +64,12 @@ class Pipeline {
|
|
|
61
64
|
writable: true,
|
|
62
65
|
value: void 0
|
|
63
66
|
});
|
|
67
|
+
Object.defineProperty(this, "multiExec", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: void 0
|
|
72
|
+
});
|
|
64
73
|
/**
|
|
65
74
|
* Send the pipeline request to upstash.
|
|
66
75
|
*
|
|
@@ -79,8 +88,9 @@ class Pipeline {
|
|
|
79
88
|
if (this.commands.length === 0) {
|
|
80
89
|
throw new Error("Pipeline is empty");
|
|
81
90
|
}
|
|
91
|
+
const path = this.multiExec ? ["multi-exec"] : ["pipeline"];
|
|
82
92
|
const res = (await this.client.request({
|
|
83
|
-
path
|
|
93
|
+
path,
|
|
84
94
|
body: Object.values(this.commands).map((c) => c.command),
|
|
85
95
|
}));
|
|
86
96
|
return res.map(({ error, result }, i) => {
|
|
@@ -127,6 +137,15 @@ class Pipeline {
|
|
|
127
137
|
writable: true,
|
|
128
138
|
value: (...args) => this.chain(new mod_js_1.BitPosCommand(args, this.commandOptions))
|
|
129
139
|
});
|
|
140
|
+
/**
|
|
141
|
+
* @see https://redis.io/commands/zdiffstore
|
|
142
|
+
*/
|
|
143
|
+
Object.defineProperty(this, "zdiffstore", {
|
|
144
|
+
enumerable: true,
|
|
145
|
+
configurable: true,
|
|
146
|
+
writable: true,
|
|
147
|
+
value: (...args) => this.chain(new zdiffstore_js_1.ZDiffStoreCommand(args, this.commandOptions))
|
|
148
|
+
});
|
|
130
149
|
/**
|
|
131
150
|
* @see https://redis.io/commands/dbsize
|
|
132
151
|
*/
|
|
@@ -253,6 +272,15 @@ class Pipeline {
|
|
|
253
272
|
writable: true,
|
|
254
273
|
value: (...args) => this.chain(new mod_js_1.GetBitCommand(args, this.commandOptions))
|
|
255
274
|
});
|
|
275
|
+
/**
|
|
276
|
+
* @see https://redis.io/commands/getdel
|
|
277
|
+
*/
|
|
278
|
+
Object.defineProperty(this, "getdel", {
|
|
279
|
+
enumerable: true,
|
|
280
|
+
configurable: true,
|
|
281
|
+
writable: true,
|
|
282
|
+
value: (...args) => this.chain(new mod_js_1.GetDelCommand(args, this.commandOptions))
|
|
283
|
+
});
|
|
256
284
|
/**
|
|
257
285
|
* @see https://redis.io/commands/getrange
|
|
258
286
|
*/
|
|
@@ -361,6 +389,15 @@ class Pipeline {
|
|
|
361
389
|
writable: true,
|
|
362
390
|
value: (key, kv) => this.chain(new mod_js_1.HMSetCommand([key, kv], this.commandOptions))
|
|
363
391
|
});
|
|
392
|
+
/**
|
|
393
|
+
* @see https://redis.io/commands/hrandfield
|
|
394
|
+
*/
|
|
395
|
+
Object.defineProperty(this, "hrandfield", {
|
|
396
|
+
enumerable: true,
|
|
397
|
+
configurable: true,
|
|
398
|
+
writable: true,
|
|
399
|
+
value: (key, count, withValues) => this.chain(new hrandfield_js_1.HRandFieldCommand([key, count, withValues], this.commandOptions))
|
|
400
|
+
});
|
|
364
401
|
/**
|
|
365
402
|
* @see https://redis.io/commands/hscan
|
|
366
403
|
*/
|
|
@@ -469,6 +506,15 @@ class Pipeline {
|
|
|
469
506
|
writable: true,
|
|
470
507
|
value: (...args) => this.chain(new mod_js_1.LLenCommand(args, this.commandOptions))
|
|
471
508
|
});
|
|
509
|
+
/**
|
|
510
|
+
* @see https://redis.io/commands/lmove
|
|
511
|
+
*/
|
|
512
|
+
Object.defineProperty(this, "lmove", {
|
|
513
|
+
enumerable: true,
|
|
514
|
+
configurable: true,
|
|
515
|
+
writable: true,
|
|
516
|
+
value: (...args) => this.chain(new mod_js_1.LMoveCommand(args, this.commandOptions))
|
|
517
|
+
});
|
|
472
518
|
/**
|
|
473
519
|
* @see https://redis.io/commands/lpop
|
|
474
520
|
*/
|
|
@@ -478,6 +524,15 @@ class Pipeline {
|
|
|
478
524
|
writable: true,
|
|
479
525
|
value: (...args) => this.chain(new mod_js_1.LPopCommand(args, this.commandOptions))
|
|
480
526
|
});
|
|
527
|
+
/**
|
|
528
|
+
* @see https://redis.io/commands/lpos
|
|
529
|
+
*/
|
|
530
|
+
Object.defineProperty(this, "lpos", {
|
|
531
|
+
enumerable: true,
|
|
532
|
+
configurable: true,
|
|
533
|
+
writable: true,
|
|
534
|
+
value: (...args) => this.chain(new mod_js_1.LPosCommand(args, this.commandOptions))
|
|
535
|
+
});
|
|
481
536
|
/**
|
|
482
537
|
* @see https://redis.io/commands/lpush
|
|
483
538
|
*/
|
|
@@ -829,6 +884,15 @@ class Pipeline {
|
|
|
829
884
|
writable: true,
|
|
830
885
|
value: (...args) => this.chain(new mod_js_1.SMembersCommand(args, this.commandOptions))
|
|
831
886
|
});
|
|
887
|
+
/**
|
|
888
|
+
* @see https://redis.io/commands/smismember
|
|
889
|
+
*/
|
|
890
|
+
Object.defineProperty(this, "smismember", {
|
|
891
|
+
enumerable: true,
|
|
892
|
+
configurable: true,
|
|
893
|
+
writable: true,
|
|
894
|
+
value: (key, members) => this.chain(new mod_js_1.SMIsMemberCommand([key, members], this.commandOptions))
|
|
895
|
+
});
|
|
832
896
|
/**
|
|
833
897
|
* @see https://redis.io/commands/smove
|
|
834
898
|
*/
|
|
@@ -1005,6 +1069,15 @@ class Pipeline {
|
|
|
1005
1069
|
writable: true,
|
|
1006
1070
|
value: (...args) => this.chain(new mod_js_1.ZLexCountCommand(args, this.commandOptions))
|
|
1007
1071
|
});
|
|
1072
|
+
/**
|
|
1073
|
+
* @see https://redis.io/commands/zmscore
|
|
1074
|
+
*/
|
|
1075
|
+
Object.defineProperty(this, "zmscore", {
|
|
1076
|
+
enumerable: true,
|
|
1077
|
+
configurable: true,
|
|
1078
|
+
writable: true,
|
|
1079
|
+
value: (...args) => this.chain(new zmscore_js_1.ZMScoreCommand(args, this.commandOptions))
|
|
1080
|
+
});
|
|
1008
1081
|
/**
|
|
1009
1082
|
* @see https://redis.io/commands/zpopmax
|
|
1010
1083
|
*/
|
|
@@ -1113,9 +1186,10 @@ class Pipeline {
|
|
|
1113
1186
|
writable: true,
|
|
1114
1187
|
value: (...args) => this.chain(new mod_js_1.ZUnionStoreCommand(args, this.commandOptions))
|
|
1115
1188
|
});
|
|
1116
|
-
this.client = client;
|
|
1189
|
+
this.client = opts.client;
|
|
1117
1190
|
this.commands = [];
|
|
1118
|
-
this.commandOptions = commandOptions;
|
|
1191
|
+
this.commandOptions = opts.commandOptions;
|
|
1192
|
+
this.multiExec = opts.multiExec ?? false;
|
|
1119
1193
|
}
|
|
1120
1194
|
/**
|
|
1121
1195
|
* Pushes a command into the pipelien and returns a chainable instance of the
|
package/script/pkg/redis.js
CHANGED
|
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Redis = void 0;
|
|
4
4
|
const mod_js_1 = require("./commands/mod.js");
|
|
5
5
|
const pipeline_js_1 = require("./pipeline.js");
|
|
6
|
+
const script_js_1 = require("./script.js");
|
|
7
|
+
const zmscore_js_1 = require("./commands/zmscore.js");
|
|
8
|
+
const zdiffstore_js_1 = require("./commands/zdiffstore.js");
|
|
6
9
|
/**
|
|
7
10
|
* Serverless redis client for upstash.
|
|
8
11
|
*/
|
|
@@ -31,6 +34,36 @@ class Redis {
|
|
|
31
34
|
writable: true,
|
|
32
35
|
value: void 0
|
|
33
36
|
});
|
|
37
|
+
/**
|
|
38
|
+
* Wrap a new middleware around the HTTP client.
|
|
39
|
+
*/
|
|
40
|
+
Object.defineProperty(this, "use", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
value: (middleware) => {
|
|
45
|
+
const makeRequest = this.client.request.bind(this.client);
|
|
46
|
+
this.client.request = (req) => middleware(req, makeRequest);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
/**
|
|
50
|
+
* Technically this is not private, we can hide it from intellisense by doing this
|
|
51
|
+
*/
|
|
52
|
+
Object.defineProperty(this, "addTelemetry", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
configurable: true,
|
|
55
|
+
writable: true,
|
|
56
|
+
value: (telemetry) => {
|
|
57
|
+
try {
|
|
58
|
+
// @ts-ignore - The `Requester` interface does not know about this method but it will be there
|
|
59
|
+
// as long as the user uses the standard HttpClient
|
|
60
|
+
this.client.mergeTelemetry(telemetry);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// ignore
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
34
67
|
/**
|
|
35
68
|
* Create a new pipeline that allows you to send requests in bulk.
|
|
36
69
|
*
|
|
@@ -40,7 +73,30 @@ class Redis {
|
|
|
40
73
|
enumerable: true,
|
|
41
74
|
configurable: true,
|
|
42
75
|
writable: true,
|
|
43
|
-
value: () => new pipeline_js_1.Pipeline(
|
|
76
|
+
value: () => new pipeline_js_1.Pipeline({
|
|
77
|
+
client: this.client,
|
|
78
|
+
commandOptions: this.opts,
|
|
79
|
+
multiExec: false,
|
|
80
|
+
})
|
|
81
|
+
});
|
|
82
|
+
/**
|
|
83
|
+
* Create a new transaction to allow executing multiple steps atomically.
|
|
84
|
+
*
|
|
85
|
+
* All the commands in a transaction are serialized and executed sequentially. A request sent by
|
|
86
|
+
* another client will never be served in the middle of the execution of a Redis Transaction. This
|
|
87
|
+
* guarantees that the commands are executed as a single isolated operation.
|
|
88
|
+
*
|
|
89
|
+
* @see {@link Pipeline}
|
|
90
|
+
*/
|
|
91
|
+
Object.defineProperty(this, "multi", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
configurable: true,
|
|
94
|
+
writable: true,
|
|
95
|
+
value: () => new pipeline_js_1.Pipeline({
|
|
96
|
+
client: this.client,
|
|
97
|
+
commandOptions: this.opts,
|
|
98
|
+
multiExec: true,
|
|
99
|
+
})
|
|
44
100
|
});
|
|
45
101
|
/**
|
|
46
102
|
* @see https://redis.io/commands/append
|
|
@@ -204,6 +260,15 @@ class Redis {
|
|
|
204
260
|
writable: true,
|
|
205
261
|
value: (...args) => new mod_js_1.GetBitCommand(args, this.opts).exec(this.client)
|
|
206
262
|
});
|
|
263
|
+
/**
|
|
264
|
+
* @see https://redis.io/commands/getdel
|
|
265
|
+
*/
|
|
266
|
+
Object.defineProperty(this, "getdel", {
|
|
267
|
+
enumerable: true,
|
|
268
|
+
configurable: true,
|
|
269
|
+
writable: true,
|
|
270
|
+
value: (...args) => new mod_js_1.GetDelCommand(args, this.opts).exec(this.client)
|
|
271
|
+
});
|
|
207
272
|
/**
|
|
208
273
|
* @see https://redis.io/commands/getrange
|
|
209
274
|
*/
|
|
@@ -312,6 +377,16 @@ class Redis {
|
|
|
312
377
|
writable: true,
|
|
313
378
|
value: (key, kv) => new mod_js_1.HMSetCommand([key, kv], this.opts).exec(this.client)
|
|
314
379
|
});
|
|
380
|
+
/**
|
|
381
|
+
* @see https://redis.io/commands/hrandfield
|
|
382
|
+
*/
|
|
383
|
+
Object.defineProperty(this, "hrandfield", {
|
|
384
|
+
enumerable: true,
|
|
385
|
+
configurable: true,
|
|
386
|
+
writable: true,
|
|
387
|
+
value: (key, count, withValues) => new mod_js_1.HRandFieldCommand([key, count, withValues], this.opts)
|
|
388
|
+
.exec(this.client)
|
|
389
|
+
});
|
|
315
390
|
/**
|
|
316
391
|
* @see https://redis.io/commands/hscan
|
|
317
392
|
*/
|
|
@@ -420,6 +495,15 @@ class Redis {
|
|
|
420
495
|
writable: true,
|
|
421
496
|
value: (...args) => new mod_js_1.LLenCommand(args, this.opts).exec(this.client)
|
|
422
497
|
});
|
|
498
|
+
/**
|
|
499
|
+
* @see https://redis.io/commands/lmove
|
|
500
|
+
*/
|
|
501
|
+
Object.defineProperty(this, "lmove", {
|
|
502
|
+
enumerable: true,
|
|
503
|
+
configurable: true,
|
|
504
|
+
writable: true,
|
|
505
|
+
value: (...args) => new mod_js_1.LMoveCommand(args, this.opts).exec(this.client)
|
|
506
|
+
});
|
|
423
507
|
/**
|
|
424
508
|
* @see https://redis.io/commands/lpop
|
|
425
509
|
*/
|
|
@@ -429,6 +513,15 @@ class Redis {
|
|
|
429
513
|
writable: true,
|
|
430
514
|
value: (...args) => new mod_js_1.LPopCommand(args, this.opts).exec(this.client)
|
|
431
515
|
});
|
|
516
|
+
/**
|
|
517
|
+
* @see https://redis.io/commands/lpos
|
|
518
|
+
*/
|
|
519
|
+
Object.defineProperty(this, "lpos", {
|
|
520
|
+
enumerable: true,
|
|
521
|
+
configurable: true,
|
|
522
|
+
writable: true,
|
|
523
|
+
value: (...args) => new mod_js_1.LPosCommand(args, this.opts).exec(this.client)
|
|
524
|
+
});
|
|
432
525
|
/**
|
|
433
526
|
* @see https://redis.io/commands/lpush
|
|
434
527
|
*/
|
|
@@ -771,6 +864,15 @@ class Redis {
|
|
|
771
864
|
writable: true,
|
|
772
865
|
value: (key, member) => new mod_js_1.SIsMemberCommand([key, member], this.opts).exec(this.client)
|
|
773
866
|
});
|
|
867
|
+
/**
|
|
868
|
+
* @see https://redis.io/commands/smismember
|
|
869
|
+
*/
|
|
870
|
+
Object.defineProperty(this, "smismember", {
|
|
871
|
+
enumerable: true,
|
|
872
|
+
configurable: true,
|
|
873
|
+
writable: true,
|
|
874
|
+
value: (key, members) => new mod_js_1.SMIsMemberCommand([key, members], this.opts).exec(this.client)
|
|
875
|
+
});
|
|
774
876
|
/**
|
|
775
877
|
* @see https://redis.io/commands/smembers
|
|
776
878
|
*/
|
|
@@ -929,6 +1031,15 @@ class Redis {
|
|
|
929
1031
|
writable: true,
|
|
930
1032
|
value: (...args) => new mod_js_1.ZCountCommand(args, this.opts).exec(this.client)
|
|
931
1033
|
});
|
|
1034
|
+
/**
|
|
1035
|
+
* @see https://redis.io/commands/zdiffstore
|
|
1036
|
+
*/
|
|
1037
|
+
Object.defineProperty(this, "zdiffstore", {
|
|
1038
|
+
enumerable: true,
|
|
1039
|
+
configurable: true,
|
|
1040
|
+
writable: true,
|
|
1041
|
+
value: (...args) => new zdiffstore_js_1.ZDiffStoreCommand(args, this.opts).exec(this.client)
|
|
1042
|
+
});
|
|
932
1043
|
/**
|
|
933
1044
|
* @see https://redis.io/commands/zincrby
|
|
934
1045
|
*/
|
|
@@ -956,6 +1067,15 @@ class Redis {
|
|
|
956
1067
|
writable: true,
|
|
957
1068
|
value: (...args) => new mod_js_1.ZLexCountCommand(args, this.opts).exec(this.client)
|
|
958
1069
|
});
|
|
1070
|
+
/**
|
|
1071
|
+
* @see https://redis.io/commands/zmscore
|
|
1072
|
+
*/
|
|
1073
|
+
Object.defineProperty(this, "zmscore", {
|
|
1074
|
+
enumerable: true,
|
|
1075
|
+
configurable: true,
|
|
1076
|
+
writable: true,
|
|
1077
|
+
value: (...args) => new zmscore_js_1.ZMScoreCommand(args, this.opts).exec(this.client)
|
|
1078
|
+
});
|
|
959
1079
|
/**
|
|
960
1080
|
* @see https://redis.io/commands/zpopmax
|
|
961
1081
|
*/
|
|
@@ -1067,5 +1187,8 @@ class Redis {
|
|
|
1067
1187
|
this.client = client;
|
|
1068
1188
|
this.opts = opts;
|
|
1069
1189
|
}
|
|
1190
|
+
createScript(script) {
|
|
1191
|
+
return new script_js_1.Script(this, script);
|
|
1192
|
+
}
|
|
1070
1193
|
}
|
|
1071
1194
|
exports.Redis = Redis;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Script = void 0;
|
|
4
|
+
const mod_js_1 = require("../deps/deno.land/x/sha1@v1.0.3/mod.js");
|
|
5
|
+
/**
|
|
6
|
+
* Creates a new script.
|
|
7
|
+
*
|
|
8
|
+
* Scripts offer the ability to optimistically try to execute a script without having to send the
|
|
9
|
+
* entire script to the server. If the script is loaded on the server, it tries again by sending
|
|
10
|
+
* the entire script. Afterwards, the script is cached on the server.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* const redis = new Redis({...})
|
|
15
|
+
*
|
|
16
|
+
* const script = redis.createScript<string>("return ARGV[1];")
|
|
17
|
+
* const arg1 = await script.eval([], ["Hello World"])
|
|
18
|
+
* assertEquals(arg1, "Hello World")
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
class Script {
|
|
22
|
+
constructor(redis, script) {
|
|
23
|
+
Object.defineProperty(this, "script", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "sha1", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: void 0
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "redis", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: void 0
|
|
40
|
+
});
|
|
41
|
+
this.redis = redis;
|
|
42
|
+
this.sha1 = this.digest(script);
|
|
43
|
+
this.script = script;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Send an `EVAL` command to redis.
|
|
47
|
+
*/
|
|
48
|
+
async eval(keys, args) {
|
|
49
|
+
return await this.redis.eval(this.script, keys, args);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Calculates the sha1 hash of the script and then calls `EVALSHA`.
|
|
53
|
+
*/
|
|
54
|
+
async evalsha(keys, args) {
|
|
55
|
+
return await this.redis.evalsha(this.sha1, keys, args);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Optimistically try to run `EVALSHA` first.
|
|
59
|
+
* If the script is not loaded in redis, it will fall back and try again with `EVAL`.
|
|
60
|
+
*
|
|
61
|
+
* Following calls will be able to use the cached script
|
|
62
|
+
*/
|
|
63
|
+
async exec(keys, args) {
|
|
64
|
+
const res = await this.redis.evalsha(this.sha1, keys, args).catch(async (err) => {
|
|
65
|
+
if (err instanceof Error &&
|
|
66
|
+
err.message.toLowerCase().includes("noscript")) {
|
|
67
|
+
return await this.redis.eval(this.script, keys, args);
|
|
68
|
+
}
|
|
69
|
+
throw err;
|
|
70
|
+
});
|
|
71
|
+
return res;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Compute the sha1 hash of the script and return its hex representation.
|
|
75
|
+
*/
|
|
76
|
+
digest(s) {
|
|
77
|
+
const hash = (0, mod_js_1.sha1)(s, "utf8", "hex");
|
|
78
|
+
return typeof hash === "string" ? hash : new TextDecoder().decode(hash);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.Script = Script;
|
|
@@ -25,7 +25,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.Redis = void 0;
|
|
27
27
|
const core = __importStar(require("../pkg/redis.js"));
|
|
28
|
-
const
|
|
28
|
+
const http_js_1 = require("../pkg/http.js");
|
|
29
|
+
const version_js_1 = require("../version.js");
|
|
29
30
|
/**
|
|
30
31
|
* Serverless redis client for upstash.
|
|
31
32
|
*/
|
|
@@ -41,7 +42,7 @@ class Redis extends core.Redis {
|
|
|
41
42
|
* });
|
|
42
43
|
* ```
|
|
43
44
|
*/
|
|
44
|
-
constructor(config) {
|
|
45
|
+
constructor(config, env) {
|
|
45
46
|
if (config.url.startsWith(" ") ||
|
|
46
47
|
config.url.endsWith(" ") ||
|
|
47
48
|
/\r|\n/.test(config.url)) {
|
|
@@ -52,9 +53,17 @@ class Redis extends core.Redis {
|
|
|
52
53
|
/\r|\n/.test(config.token)) {
|
|
53
54
|
console.warn("The redis token contains whitespace or newline, which can cause errors!");
|
|
54
55
|
}
|
|
55
|
-
const
|
|
56
|
+
const telemetry = {};
|
|
57
|
+
if (!env?.UPSTASH_DISABLE_TELEMETRY) {
|
|
58
|
+
telemetry.platform = "cloudflare";
|
|
59
|
+
telemetry.sdk = `@upstash/redis@${version_js_1.VERSION}`;
|
|
60
|
+
}
|
|
61
|
+
const client = new http_js_1.HttpClient({
|
|
62
|
+
retry: config.retry,
|
|
56
63
|
baseUrl: config.url,
|
|
57
64
|
headers: { authorization: `Bearer ${config.token}` },
|
|
65
|
+
responseEncoding: config.responseEncoding,
|
|
66
|
+
telemetry,
|
|
58
67
|
});
|
|
59
68
|
super(client, {
|
|
60
69
|
automaticDeserialization: config.automaticDeserialization,
|
|
@@ -70,9 +79,8 @@ class Redis extends core.Redis {
|
|
|
70
79
|
* ```ts
|
|
71
80
|
* const redis = Redis.fromEnv(env)
|
|
72
81
|
* ```
|
|
73
|
-
*
|
|
74
82
|
*/
|
|
75
|
-
static fromEnv(env) {
|
|
83
|
+
static fromEnv(env, opts) {
|
|
76
84
|
// @ts-ignore These will be defined by cloudflare
|
|
77
85
|
const url = env?.UPSTASH_REDIS_REST_URL ?? UPSTASH_REDIS_REST_URL;
|
|
78
86
|
// @ts-ignore These will be defined by cloudflare
|
|
@@ -83,27 +91,7 @@ class Redis extends core.Redis {
|
|
|
83
91
|
if (!token) {
|
|
84
92
|
throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`");
|
|
85
93
|
}
|
|
86
|
-
return new Redis({ url, token });
|
|
94
|
+
return new Redis({ ...opts, url, token }, env);
|
|
87
95
|
}
|
|
88
96
|
}
|
|
89
97
|
exports.Redis = Redis;
|
|
90
|
-
function defaultRequester(config) {
|
|
91
|
-
return {
|
|
92
|
-
request: async function (req) {
|
|
93
|
-
if (!req.path) {
|
|
94
|
-
req.path = [];
|
|
95
|
-
}
|
|
96
|
-
const res = await fetch([config.baseUrl, ...req.path].join("/"), {
|
|
97
|
-
method: "POST",
|
|
98
|
-
headers: { "Content-Type": "application/json", ...config.headers },
|
|
99
|
-
body: JSON.stringify(req.body),
|
|
100
|
-
keepalive: true,
|
|
101
|
-
});
|
|
102
|
-
const body = (await res.json());
|
|
103
|
-
if (!res.ok) {
|
|
104
|
-
throw new error_js_1.UpstashError(body.error);
|
|
105
|
-
}
|
|
106
|
-
return body;
|
|
107
|
-
},
|
|
108
|
-
};
|
|
109
|
-
}
|