@upstash/redis 0.0.0-ci.ba685120 → 0.0.0-ci.bafaa360-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.
Files changed (91) hide show
  1. package/README.md +19 -17
  2. package/esm/deps/deno.land/x/base64@v0.2.1/base.js +100 -0
  3. package/esm/deps/deno.land/x/base64@v0.2.1/base64url.js +9 -0
  4. package/esm/deps/deno.land/x/sha1@v1.0.3/deps.js +1 -0
  5. package/esm/deps/deno.land/x/sha1@v1.0.3/mod.js +191 -0
  6. package/esm/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +50 -0
  7. package/esm/pkg/commands/getdel.js +9 -0
  8. package/esm/pkg/commands/hgetall.js +0 -4
  9. package/esm/pkg/commands/hrandfield.js +39 -0
  10. package/esm/pkg/commands/lmove.js +9 -0
  11. package/esm/pkg/commands/lpos.js +19 -0
  12. package/esm/pkg/commands/mod.js +5 -0
  13. package/esm/pkg/commands/scan.js +3 -0
  14. package/esm/pkg/commands/sdiffstore.js +1 -1
  15. package/esm/pkg/commands/set.js +16 -4
  16. package/esm/pkg/commands/smismember.js +9 -0
  17. package/esm/pkg/commands/zdiffstore.js +9 -0
  18. package/esm/pkg/commands/zmscore.js +10 -0
  19. package/esm/pkg/commands/zrange.js +6 -0
  20. package/esm/pkg/http.js +99 -2
  21. package/esm/pkg/pipeline.js +80 -6
  22. package/esm/pkg/redis.js +125 -2
  23. package/esm/pkg/script.js +77 -0
  24. package/esm/platforms/cloudflare.js +10 -2
  25. package/esm/platforms/fastly.js +1 -0
  26. package/esm/platforms/node_with_fetch.js +27 -1
  27. package/esm/platforms/nodejs.js +22 -1
  28. package/esm/version.js +1 -0
  29. package/package.json +1 -39
  30. package/script/deps/deno.land/x/base64@v0.2.1/base.js +104 -0
  31. package/script/deps/deno.land/x/base64@v0.2.1/base64url.js +13 -0
  32. package/script/deps/deno.land/x/sha1@v1.0.3/deps.js +6 -0
  33. package/script/deps/deno.land/x/sha1@v1.0.3/mod.js +196 -0
  34. package/script/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +55 -0
  35. package/script/pkg/commands/getdel.js +13 -0
  36. package/script/pkg/commands/hgetall.js +0 -4
  37. package/script/pkg/commands/hrandfield.js +43 -0
  38. package/script/pkg/commands/lmove.js +13 -0
  39. package/script/pkg/commands/lpos.js +23 -0
  40. package/script/pkg/commands/mod.js +5 -0
  41. package/script/pkg/commands/scan.js +3 -0
  42. package/script/pkg/commands/sdiffstore.js +1 -1
  43. package/script/pkg/commands/set.js +16 -4
  44. package/script/pkg/commands/smismember.js +13 -0
  45. package/script/pkg/commands/zdiffstore.js +13 -0
  46. package/script/pkg/commands/zmscore.js +14 -0
  47. package/script/pkg/commands/zrange.js +6 -0
  48. package/script/pkg/http.js +99 -2
  49. package/script/pkg/pipeline.js +79 -5
  50. package/script/pkg/redis.js +124 -1
  51. package/script/pkg/script.js +81 -0
  52. package/script/platforms/cloudflare.js +10 -2
  53. package/script/platforms/fastly.js +1 -0
  54. package/script/platforms/node_with_fetch.js +27 -1
  55. package/script/platforms/nodejs.js +22 -1
  56. package/script/version.js +4 -0
  57. package/types/deps/deno.land/x/base64@v0.2.1/base.d.ts +5 -0
  58. package/types/deps/deno.land/x/base64@v0.2.1/base64url.d.ts +1 -0
  59. package/types/deps/deno.land/x/sha1@v1.0.3/deps.d.ts +1 -0
  60. package/types/deps/deno.land/x/sha1@v1.0.3/mod.d.ts +26 -0
  61. package/types/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.d.ts +3 -0
  62. package/types/pkg/commands/bitop.d.ts +0 -1
  63. package/types/pkg/commands/bitpos.d.ts +1 -1
  64. package/types/pkg/commands/getdel.d.ts +7 -0
  65. package/types/pkg/commands/hrandfield.d.ts +9 -0
  66. package/types/pkg/commands/lmove.d.ts +12 -0
  67. package/types/pkg/commands/lpop.d.ts +1 -1
  68. package/types/pkg/commands/lpos.d.ts +15 -0
  69. package/types/pkg/commands/mget.d.ts +1 -1
  70. package/types/pkg/commands/mod.d.ts +5 -0
  71. package/types/pkg/commands/rpop.d.ts +2 -2
  72. package/types/pkg/commands/scan.d.ts +1 -0
  73. package/types/pkg/commands/sdiffstore.d.ts +1 -1
  74. package/types/pkg/commands/set.d.ts +31 -2
  75. package/types/pkg/commands/smembers.d.ts +2 -2
  76. package/types/pkg/commands/smismember.d.ts +7 -0
  77. package/types/pkg/commands/spop.d.ts +2 -2
  78. package/types/pkg/commands/zadd.d.ts +1 -1
  79. package/types/pkg/commands/zdiffstore.d.ts +7 -0
  80. package/types/pkg/commands/zmscore.d.ts +7 -0
  81. package/types/pkg/commands/zrange.d.ts +7 -0
  82. package/types/pkg/http.d.ts +38 -4
  83. package/types/pkg/pipeline.d.ts +46 -9
  84. package/types/pkg/redis.d.ts +65 -9
  85. package/types/pkg/script.d.ts +42 -0
  86. package/types/pkg/types.d.ts +17 -0
  87. package/types/platforms/cloudflare.d.ts +8 -7
  88. package/types/platforms/fastly.d.ts +2 -6
  89. package/types/platforms/node_with_fetch.d.ts +2 -21
  90. package/types/platforms/nodejs.d.ts +3 -6
  91. package/types/version.d.ts +1 -0
package/esm/pkg/http.js CHANGED
@@ -25,9 +25,28 @@ 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
- this.headers = { "Content-Type": "application/json", ...config.headers };
30
- this.options = { backend: config.options?.backend };
34
+ this.headers = {
35
+ "Content-Type": "application/json",
36
+ ...config.headers,
37
+ };
38
+ if (config.telemetry?.runtime) {
39
+ this.headers["Upstash-Telemetry-Runtime"] = config.telemetry.runtime;
40
+ }
41
+ if (config.telemetry?.platform) {
42
+ this.headers["Upstash-Telemetry-Platform"] = config.telemetry.platform;
43
+ }
44
+ if (config.telemetry?.sdk) {
45
+ this.headers["Upstash-Telemetry-Sdk"] = config.telemetry.sdk;
46
+ }
47
+ if (this.options.responseEncoding === "base64") {
48
+ this.headers["Upstash-Encoding"] = "base64";
49
+ }
31
50
  if (typeof config?.retry === "boolean" && config?.retry === false) {
32
51
  this.retry = {
33
52
  attempts: 1,
@@ -42,12 +61,30 @@ export class HttpClient {
42
61
  };
43
62
  }
44
63
  }
64
+ mergeTelemetry(telemetry) {
65
+ function merge(obj, key, value) {
66
+ if (!value) {
67
+ return obj;
68
+ }
69
+ if (obj[key]) {
70
+ obj[key] = [obj[key], value].join(",");
71
+ }
72
+ else {
73
+ obj[key] = value;
74
+ }
75
+ return obj;
76
+ }
77
+ this.headers = merge(this.headers, "Upstash-Telemetry-Runtime", telemetry.runtime);
78
+ this.headers = merge(this.headers, "Upstash-Telemetry-Platform", telemetry.platform);
79
+ this.headers = merge(this.headers, "Upstash-Telemetry-Sdk", telemetry.sdk);
80
+ }
45
81
  async request(req) {
46
82
  const requestOptions = {
47
83
  method: "POST",
48
84
  headers: this.headers,
49
85
  body: JSON.stringify(req.body),
50
86
  keepalive: true,
87
+ agent: this.options?.agent,
51
88
  /**
52
89
  * Fastly specific
53
90
  */
@@ -72,6 +109,66 @@ export class HttpClient {
72
109
  if (!res.ok) {
73
110
  throw new UpstashError(body.error);
74
111
  }
112
+ if (this.options?.responseEncoding === "base64") {
113
+ return Array.isArray(body) ? body.map(decode) : decode(body);
114
+ }
75
115
  return body;
76
116
  }
77
117
  }
118
+ function base64decode(b64) {
119
+ let dec = "";
120
+ try {
121
+ /**
122
+ * Using only atob() is not enough because it doesn't work with unicode characters
123
+ */
124
+ const binString = atob(b64);
125
+ const size = binString.length;
126
+ const bytes = new Uint8Array(size);
127
+ for (let i = 0; i < size; i++) {
128
+ bytes[i] = binString.charCodeAt(i);
129
+ }
130
+ dec = new TextDecoder().decode(bytes);
131
+ }
132
+ catch {
133
+ dec = b64;
134
+ }
135
+ return dec;
136
+ // try {
137
+ // return decodeURIComponent(dec);
138
+ // } catch {
139
+ // return dec;
140
+ // }
141
+ }
142
+ function decode(raw) {
143
+ let result = undefined;
144
+ switch (typeof raw.result) {
145
+ case "undefined":
146
+ return raw;
147
+ case "number": {
148
+ result = raw.result;
149
+ break;
150
+ }
151
+ case "object": {
152
+ if (Array.isArray(raw.result)) {
153
+ result = raw.result.map((v) => typeof v === "string"
154
+ ? base64decode(v)
155
+ : Array.isArray(v)
156
+ ? v.map(base64decode)
157
+ : v);
158
+ }
159
+ else {
160
+ // If it's not an array it must be null
161
+ // Apparently null is an object in javascript
162
+ result = null;
163
+ }
164
+ break;
165
+ }
166
+ case "string": {
167
+ result = raw.result === "OK" ? "OK" : base64decode(raw.result);
168
+ break;
169
+ }
170
+ default:
171
+ break;
172
+ }
173
+ return { result, error: raw.error };
174
+ }
@@ -1,5 +1,8 @@
1
- import { AppendCommand, BitCountCommand, BitOpCommand, BitPosCommand, DBSizeCommand, DecrByCommand, DecrCommand, DelCommand, EchoCommand, EvalCommand, EvalshaCommand, ExistsCommand, ExpireAtCommand, ExpireCommand, FlushAllCommand, FlushDBCommand, GetBitCommand, GetCommand, GetRangeCommand, GetSetCommand, HDelCommand, HExistsCommand, HGetAllCommand, HGetCommand, HIncrByCommand, HIncrByFloatCommand, HKeysCommand, HLenCommand, HMGetCommand, HMSetCommand, HScanCommand, HSetCommand, HSetNXCommand, HStrLenCommand, HValsCommand, IncrByCommand, IncrByFloatCommand, IncrCommand, KeysCommand, LIndexCommand, LInsertCommand, LLenCommand, LPopCommand, LPushCommand, LPushXCommand, LRangeCommand, LRemCommand, LSetCommand, LTrimCommand, MGetCommand, MSetCommand, MSetNXCommand, PersistCommand, PExpireAtCommand, PExpireCommand, PingCommand, PSetEXCommand, PTtlCommand, PublishCommand, RandomKeyCommand, RenameCommand, RenameNXCommand, RPopCommand, RPushCommand, RPushXCommand, SAddCommand, ScanCommand, SCardCommand, ScriptExistsCommand, ScriptFlushCommand, ScriptLoadCommand, SDiffCommand, SDiffStoreCommand, SetBitCommand, SetCommand, SetExCommand, SetNxCommand, SetRangeCommand, SInterCommand, SInterStoreCommand, SIsMemberCommand, SMembersCommand, SMoveCommand, SPopCommand, SRandMemberCommand, SRemCommand, SScanCommand, StrLenCommand, SUnionCommand, SUnionStoreCommand, TimeCommand, TouchCommand, TtlCommand, TypeCommand, UnlinkCommand, ZAddCommand, ZCardCommand, ZCountCommand, ZIncrByCommand, ZInterStoreCommand, ZLexCountCommand, ZPopMaxCommand, ZPopMinCommand, ZRangeCommand, ZRankCommand, ZRemCommand, ZRemRangeByLexCommand, ZRemRangeByRankCommand, ZRemRangeByScoreCommand, ZRevRankCommand, ZScanCommand, ZScoreCommand, ZUnionStoreCommand, } from "./commands/mod.js";
1
+ import { AppendCommand, BitCountCommand, BitOpCommand, BitPosCommand, DBSizeCommand, DecrByCommand, DecrCommand, DelCommand, EchoCommand, EvalCommand, EvalshaCommand, ExistsCommand, ExpireAtCommand, ExpireCommand, FlushAllCommand, FlushDBCommand, GetBitCommand, GetCommand, GetDelCommand, GetRangeCommand, GetSetCommand, HDelCommand, HExistsCommand, HGetAllCommand, HGetCommand, HIncrByCommand, HIncrByFloatCommand, HKeysCommand, HLenCommand, HMGetCommand, HMSetCommand, HScanCommand, HSetCommand, HSetNXCommand, HStrLenCommand, HValsCommand, IncrByCommand, IncrByFloatCommand, IncrCommand, KeysCommand, LIndexCommand, LInsertCommand, LLenCommand, LMoveCommand, LPopCommand, LPosCommand, LPushCommand, LPushXCommand, LRangeCommand, LRemCommand, LSetCommand, LTrimCommand, MGetCommand, MSetCommand, MSetNXCommand, PersistCommand, PExpireAtCommand, PExpireCommand, PingCommand, PSetEXCommand, PTtlCommand, PublishCommand, RandomKeyCommand, RenameCommand, RenameNXCommand, RPopCommand, RPushCommand, RPushXCommand, SAddCommand, ScanCommand, SCardCommand, ScriptExistsCommand, ScriptFlushCommand, ScriptLoadCommand, SDiffCommand, SDiffStoreCommand, SetBitCommand, SetCommand, SetExCommand, SetNxCommand, SetRangeCommand, SInterCommand, SInterStoreCommand, SIsMemberCommand, SMembersCommand, SMIsMemberCommand, SMoveCommand, SPopCommand, SRandMemberCommand, SRemCommand, SScanCommand, StrLenCommand, SUnionCommand, SUnionStoreCommand, TimeCommand, TouchCommand, TtlCommand, TypeCommand, UnlinkCommand, ZAddCommand, ZCardCommand, ZCountCommand, ZIncrByCommand, ZInterStoreCommand, ZLexCountCommand, ZPopMaxCommand, ZPopMinCommand, ZRangeCommand, ZRankCommand, ZRemCommand, ZRemRangeByLexCommand, ZRemRangeByRankCommand, ZRemRangeByScoreCommand, ZRevRankCommand, ZScanCommand, ZScoreCommand, ZUnionStoreCommand, } from "./commands/mod.js";
2
2
  import { UpstashError } from "./error.js";
3
+ import { ZMScoreCommand } from "./commands/zmscore.js";
4
+ import { HRandFieldCommand } from "./commands/hrandfield.js";
5
+ import { ZDiffStoreCommand } from "./commands/zdiffstore.js";
3
6
  /**
4
7
  * Upstash REST API supports command pipelining to send multiple commands in
5
8
  * batch, instead of sending each command one by one and waiting for a response.
@@ -16,7 +19,7 @@ import { UpstashError } from "./error.js";
16
19
  * **Examples:**
17
20
  *
18
21
  * ```ts
19
- * const p = redis.pipeline()
22
+ * const p = redis.pipeline() // or redis.multi()
20
23
  * p.set("key","value")
21
24
  * p.get("key")
22
25
  * const res = await p.exec()
@@ -39,7 +42,7 @@ import { UpstashError } from "./error.js";
39
42
  * ```
40
43
  */
41
44
  export class Pipeline {
42
- constructor(client, commandOptions) {
45
+ constructor(opts) {
43
46
  Object.defineProperty(this, "client", {
44
47
  enumerable: true,
45
48
  configurable: true,
@@ -58,6 +61,12 @@ export class Pipeline {
58
61
  writable: true,
59
62
  value: void 0
60
63
  });
64
+ Object.defineProperty(this, "multiExec", {
65
+ enumerable: true,
66
+ configurable: true,
67
+ writable: true,
68
+ value: void 0
69
+ });
61
70
  /**
62
71
  * Send the pipeline request to upstash.
63
72
  *
@@ -76,8 +85,9 @@ export class Pipeline {
76
85
  if (this.commands.length === 0) {
77
86
  throw new Error("Pipeline is empty");
78
87
  }
88
+ const path = this.multiExec ? ["multi-exec"] : ["pipeline"];
79
89
  const res = (await this.client.request({
80
- path: ["pipeline"],
90
+ path,
81
91
  body: Object.values(this.commands).map((c) => c.command),
82
92
  }));
83
93
  return res.map(({ error, result }, i) => {
@@ -124,6 +134,15 @@ export class Pipeline {
124
134
  writable: true,
125
135
  value: (...args) => this.chain(new BitPosCommand(args, this.commandOptions))
126
136
  });
137
+ /**
138
+ * @see https://redis.io/commands/zdiffstore
139
+ */
140
+ Object.defineProperty(this, "zdiffstore", {
141
+ enumerable: true,
142
+ configurable: true,
143
+ writable: true,
144
+ value: (...args) => this.chain(new ZDiffStoreCommand(args, this.commandOptions))
145
+ });
127
146
  /**
128
147
  * @see https://redis.io/commands/dbsize
129
148
  */
@@ -250,6 +269,15 @@ export class Pipeline {
250
269
  writable: true,
251
270
  value: (...args) => this.chain(new GetBitCommand(args, this.commandOptions))
252
271
  });
272
+ /**
273
+ * @see https://redis.io/commands/getdel
274
+ */
275
+ Object.defineProperty(this, "getdel", {
276
+ enumerable: true,
277
+ configurable: true,
278
+ writable: true,
279
+ value: (...args) => this.chain(new GetDelCommand(args, this.commandOptions))
280
+ });
253
281
  /**
254
282
  * @see https://redis.io/commands/getrange
255
283
  */
@@ -358,6 +386,15 @@ export class Pipeline {
358
386
  writable: true,
359
387
  value: (key, kv) => this.chain(new HMSetCommand([key, kv], this.commandOptions))
360
388
  });
389
+ /**
390
+ * @see https://redis.io/commands/hrandfield
391
+ */
392
+ Object.defineProperty(this, "hrandfield", {
393
+ enumerable: true,
394
+ configurable: true,
395
+ writable: true,
396
+ value: (key, count, withValues) => this.chain(new HRandFieldCommand([key, count, withValues], this.commandOptions))
397
+ });
361
398
  /**
362
399
  * @see https://redis.io/commands/hscan
363
400
  */
@@ -466,6 +503,15 @@ export class Pipeline {
466
503
  writable: true,
467
504
  value: (...args) => this.chain(new LLenCommand(args, this.commandOptions))
468
505
  });
506
+ /**
507
+ * @see https://redis.io/commands/lmove
508
+ */
509
+ Object.defineProperty(this, "lmove", {
510
+ enumerable: true,
511
+ configurable: true,
512
+ writable: true,
513
+ value: (...args) => this.chain(new LMoveCommand(args, this.commandOptions))
514
+ });
469
515
  /**
470
516
  * @see https://redis.io/commands/lpop
471
517
  */
@@ -475,6 +521,15 @@ export class Pipeline {
475
521
  writable: true,
476
522
  value: (...args) => this.chain(new LPopCommand(args, this.commandOptions))
477
523
  });
524
+ /**
525
+ * @see https://redis.io/commands/lpos
526
+ */
527
+ Object.defineProperty(this, "lpos", {
528
+ enumerable: true,
529
+ configurable: true,
530
+ writable: true,
531
+ value: (...args) => this.chain(new LPosCommand(args, this.commandOptions))
532
+ });
478
533
  /**
479
534
  * @see https://redis.io/commands/lpush
480
535
  */
@@ -826,6 +881,15 @@ export class Pipeline {
826
881
  writable: true,
827
882
  value: (...args) => this.chain(new SMembersCommand(args, this.commandOptions))
828
883
  });
884
+ /**
885
+ * @see https://redis.io/commands/smismember
886
+ */
887
+ Object.defineProperty(this, "smismember", {
888
+ enumerable: true,
889
+ configurable: true,
890
+ writable: true,
891
+ value: (key, members) => this.chain(new SMIsMemberCommand([key, members], this.commandOptions))
892
+ });
829
893
  /**
830
894
  * @see https://redis.io/commands/smove
831
895
  */
@@ -1002,6 +1066,15 @@ export class Pipeline {
1002
1066
  writable: true,
1003
1067
  value: (...args) => this.chain(new ZLexCountCommand(args, this.commandOptions))
1004
1068
  });
1069
+ /**
1070
+ * @see https://redis.io/commands/zmscore
1071
+ */
1072
+ Object.defineProperty(this, "zmscore", {
1073
+ enumerable: true,
1074
+ configurable: true,
1075
+ writable: true,
1076
+ value: (...args) => this.chain(new ZMScoreCommand(args, this.commandOptions))
1077
+ });
1005
1078
  /**
1006
1079
  * @see https://redis.io/commands/zpopmax
1007
1080
  */
@@ -1110,9 +1183,10 @@ export class Pipeline {
1110
1183
  writable: true,
1111
1184
  value: (...args) => this.chain(new ZUnionStoreCommand(args, this.commandOptions))
1112
1185
  });
1113
- this.client = client;
1186
+ this.client = opts.client;
1114
1187
  this.commands = [];
1115
- this.commandOptions = commandOptions;
1188
+ this.commandOptions = opts.commandOptions;
1189
+ this.multiExec = opts.multiExec ?? false;
1116
1190
  }
1117
1191
  /**
1118
1192
  * Pushes a command into the pipelien and returns a chainable instance of the
package/esm/pkg/redis.js CHANGED
@@ -1,5 +1,8 @@
1
- import { AppendCommand, BitCountCommand, BitOpCommand, BitPosCommand, DBSizeCommand, DecrByCommand, DecrCommand, DelCommand, EchoCommand, EvalCommand, EvalshaCommand, ExistsCommand, ExpireAtCommand, ExpireCommand, FlushAllCommand, FlushDBCommand, GetBitCommand, GetCommand, GetRangeCommand, GetSetCommand, HDelCommand, HExistsCommand, HGetAllCommand, HGetCommand, HIncrByCommand, HIncrByFloatCommand, HKeysCommand, HLenCommand, HMGetCommand, HMSetCommand, HScanCommand, HSetCommand, HSetNXCommand, HStrLenCommand, HValsCommand, IncrByCommand, IncrByFloatCommand, IncrCommand, KeysCommand, LIndexCommand, LInsertCommand, LLenCommand, LPopCommand, LPushCommand, LPushXCommand, LRangeCommand, LRemCommand, LSetCommand, LTrimCommand, MGetCommand, MSetCommand, MSetNXCommand, PersistCommand, PExpireAtCommand, PExpireCommand, PingCommand, PSetEXCommand, PTtlCommand, PublishCommand, RandomKeyCommand, RenameCommand, RenameNXCommand, RPopCommand, RPushCommand, RPushXCommand, SAddCommand, ScanCommand, SCardCommand, ScriptExistsCommand, ScriptFlushCommand, ScriptLoadCommand, SDiffCommand, SDiffStoreCommand, SetBitCommand, SetCommand, SetExCommand, SetNxCommand, SetRangeCommand, SInterCommand, SInterStoreCommand, SIsMemberCommand, SMembersCommand, SMoveCommand, SPopCommand, SRandMemberCommand, SRemCommand, SScanCommand, StrLenCommand, SUnionCommand, SUnionStoreCommand, TimeCommand, TouchCommand, TtlCommand, TypeCommand, UnlinkCommand, ZAddCommand, ZCardCommand, ZCountCommand, ZIncrByCommand, ZInterStoreCommand, ZLexCountCommand, ZPopMaxCommand, ZPopMinCommand, ZRangeCommand, ZRankCommand, ZRemCommand, ZRemRangeByLexCommand, ZRemRangeByRankCommand, ZRemRangeByScoreCommand, ZRevRankCommand, ZScanCommand, ZScoreCommand, ZUnionStoreCommand, } from "./commands/mod.js";
1
+ import { AppendCommand, BitCountCommand, BitOpCommand, BitPosCommand, DBSizeCommand, DecrByCommand, DecrCommand, DelCommand, EchoCommand, EvalCommand, EvalshaCommand, ExistsCommand, ExpireAtCommand, ExpireCommand, FlushAllCommand, FlushDBCommand, GetBitCommand, GetCommand, GetDelCommand, GetRangeCommand, GetSetCommand, HDelCommand, HExistsCommand, HGetAllCommand, HGetCommand, HIncrByCommand, HIncrByFloatCommand, HKeysCommand, HLenCommand, HMGetCommand, HMSetCommand, HRandFieldCommand, HScanCommand, HSetCommand, HSetNXCommand, HStrLenCommand, HValsCommand, IncrByCommand, IncrByFloatCommand, IncrCommand, KeysCommand, LIndexCommand, LInsertCommand, LLenCommand, LMoveCommand, LPopCommand, LPosCommand, LPushCommand, LPushXCommand, LRangeCommand, LRemCommand, LSetCommand, LTrimCommand, MGetCommand, MSetCommand, MSetNXCommand, PersistCommand, PExpireAtCommand, PExpireCommand, PingCommand, PSetEXCommand, PTtlCommand, PublishCommand, RandomKeyCommand, RenameCommand, RenameNXCommand, RPopCommand, RPushCommand, RPushXCommand, SAddCommand, ScanCommand, SCardCommand, ScriptExistsCommand, ScriptFlushCommand, ScriptLoadCommand, SDiffCommand, SDiffStoreCommand, SetBitCommand, SetCommand, SetExCommand, SetNxCommand, SetRangeCommand, SInterCommand, SInterStoreCommand, SIsMemberCommand, SMembersCommand, SMIsMemberCommand, SMoveCommand, SPopCommand, SRandMemberCommand, SRemCommand, SScanCommand, StrLenCommand, SUnionCommand, SUnionStoreCommand, TimeCommand, TouchCommand, TtlCommand, TypeCommand, UnlinkCommand, ZAddCommand, ZCardCommand, ZCountCommand, ZIncrByCommand, ZInterStoreCommand, ZLexCountCommand, ZPopMaxCommand, ZPopMinCommand, ZRangeCommand, ZRankCommand, ZRemCommand, ZRemRangeByLexCommand, ZRemRangeByRankCommand, ZRemRangeByScoreCommand, ZRevRankCommand, ZScanCommand, ZScoreCommand, ZUnionStoreCommand, } from "./commands/mod.js";
2
2
  import { Pipeline } from "./pipeline.js";
3
+ import { Script } from "./script.js";
4
+ import { ZMScoreCommand } from "./commands/zmscore.js";
5
+ import { ZDiffStoreCommand } from "./commands/zdiffstore.js";
3
6
  /**
4
7
  * Serverless redis client for upstash.
5
8
  */
@@ -28,6 +31,36 @@ export class Redis {
28
31
  writable: true,
29
32
  value: void 0
30
33
  });
34
+ /**
35
+ * Wrap a new middleware around the HTTP client.
36
+ */
37
+ Object.defineProperty(this, "use", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: (middleware) => {
42
+ const makeRequest = this.client.request.bind(this.client);
43
+ this.client.request = (req) => middleware(req, makeRequest);
44
+ }
45
+ });
46
+ /**
47
+ * Technically this is not private, we can hide it from intellisense by doing this
48
+ */
49
+ Object.defineProperty(this, "addTelemetry", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: (telemetry) => {
54
+ try {
55
+ // @ts-ignore - The `Requester` interface does not know about this method but it will be there
56
+ // as long as the user uses the standard HttpClient
57
+ this.client.mergeTelemetry(telemetry);
58
+ }
59
+ catch {
60
+ // ignore
61
+ }
62
+ }
63
+ });
31
64
  /**
32
65
  * Create a new pipeline that allows you to send requests in bulk.
33
66
  *
@@ -37,7 +70,30 @@ export class Redis {
37
70
  enumerable: true,
38
71
  configurable: true,
39
72
  writable: true,
40
- value: () => new Pipeline(this.client, this.opts)
73
+ value: () => new Pipeline({
74
+ client: this.client,
75
+ commandOptions: this.opts,
76
+ multiExec: false,
77
+ })
78
+ });
79
+ /**
80
+ * Create a new transaction to allow executing multiple steps atomically.
81
+ *
82
+ * All the commands in a transaction are serialized and executed sequentially. A request sent by
83
+ * another client will never be served in the middle of the execution of a Redis Transaction. This
84
+ * guarantees that the commands are executed as a single isolated operation.
85
+ *
86
+ * @see {@link Pipeline}
87
+ */
88
+ Object.defineProperty(this, "multi", {
89
+ enumerable: true,
90
+ configurable: true,
91
+ writable: true,
92
+ value: () => new Pipeline({
93
+ client: this.client,
94
+ commandOptions: this.opts,
95
+ multiExec: true,
96
+ })
41
97
  });
42
98
  /**
43
99
  * @see https://redis.io/commands/append
@@ -201,6 +257,15 @@ export class Redis {
201
257
  writable: true,
202
258
  value: (...args) => new GetBitCommand(args, this.opts).exec(this.client)
203
259
  });
260
+ /**
261
+ * @see https://redis.io/commands/getdel
262
+ */
263
+ Object.defineProperty(this, "getdel", {
264
+ enumerable: true,
265
+ configurable: true,
266
+ writable: true,
267
+ value: (...args) => new GetDelCommand(args, this.opts).exec(this.client)
268
+ });
204
269
  /**
205
270
  * @see https://redis.io/commands/getrange
206
271
  */
@@ -309,6 +374,16 @@ export class Redis {
309
374
  writable: true,
310
375
  value: (key, kv) => new HMSetCommand([key, kv], this.opts).exec(this.client)
311
376
  });
377
+ /**
378
+ * @see https://redis.io/commands/hrandfield
379
+ */
380
+ Object.defineProperty(this, "hrandfield", {
381
+ enumerable: true,
382
+ configurable: true,
383
+ writable: true,
384
+ value: (key, count, withValues) => new HRandFieldCommand([key, count, withValues], this.opts)
385
+ .exec(this.client)
386
+ });
312
387
  /**
313
388
  * @see https://redis.io/commands/hscan
314
389
  */
@@ -417,6 +492,15 @@ export class Redis {
417
492
  writable: true,
418
493
  value: (...args) => new LLenCommand(args, this.opts).exec(this.client)
419
494
  });
495
+ /**
496
+ * @see https://redis.io/commands/lmove
497
+ */
498
+ Object.defineProperty(this, "lmove", {
499
+ enumerable: true,
500
+ configurable: true,
501
+ writable: true,
502
+ value: (...args) => new LMoveCommand(args, this.opts).exec(this.client)
503
+ });
420
504
  /**
421
505
  * @see https://redis.io/commands/lpop
422
506
  */
@@ -426,6 +510,15 @@ export class Redis {
426
510
  writable: true,
427
511
  value: (...args) => new LPopCommand(args, this.opts).exec(this.client)
428
512
  });
513
+ /**
514
+ * @see https://redis.io/commands/lpos
515
+ */
516
+ Object.defineProperty(this, "lpos", {
517
+ enumerable: true,
518
+ configurable: true,
519
+ writable: true,
520
+ value: (...args) => new LPosCommand(args, this.opts).exec(this.client)
521
+ });
429
522
  /**
430
523
  * @see https://redis.io/commands/lpush
431
524
  */
@@ -768,6 +861,15 @@ export class Redis {
768
861
  writable: true,
769
862
  value: (key, member) => new SIsMemberCommand([key, member], this.opts).exec(this.client)
770
863
  });
864
+ /**
865
+ * @see https://redis.io/commands/smismember
866
+ */
867
+ Object.defineProperty(this, "smismember", {
868
+ enumerable: true,
869
+ configurable: true,
870
+ writable: true,
871
+ value: (key, members) => new SMIsMemberCommand([key, members], this.opts).exec(this.client)
872
+ });
771
873
  /**
772
874
  * @see https://redis.io/commands/smembers
773
875
  */
@@ -926,6 +1028,15 @@ export class Redis {
926
1028
  writable: true,
927
1029
  value: (...args) => new ZCountCommand(args, this.opts).exec(this.client)
928
1030
  });
1031
+ /**
1032
+ * @see https://redis.io/commands/zdiffstore
1033
+ */
1034
+ Object.defineProperty(this, "zdiffstore", {
1035
+ enumerable: true,
1036
+ configurable: true,
1037
+ writable: true,
1038
+ value: (...args) => new ZDiffStoreCommand(args, this.opts).exec(this.client)
1039
+ });
929
1040
  /**
930
1041
  * @see https://redis.io/commands/zincrby
931
1042
  */
@@ -953,6 +1064,15 @@ export class Redis {
953
1064
  writable: true,
954
1065
  value: (...args) => new ZLexCountCommand(args, this.opts).exec(this.client)
955
1066
  });
1067
+ /**
1068
+ * @see https://redis.io/commands/zmscore
1069
+ */
1070
+ Object.defineProperty(this, "zmscore", {
1071
+ enumerable: true,
1072
+ configurable: true,
1073
+ writable: true,
1074
+ value: (...args) => new ZMScoreCommand(args, this.opts).exec(this.client)
1075
+ });
956
1076
  /**
957
1077
  * @see https://redis.io/commands/zpopmax
958
1078
  */
@@ -1064,4 +1184,7 @@ export class Redis {
1064
1184
  this.client = client;
1065
1185
  this.opts = opts;
1066
1186
  }
1187
+ createScript(script) {
1188
+ return new Script(this, script);
1189
+ }
1067
1190
  }
@@ -0,0 +1,77 @@
1
+ import { sha1 as digest } from "../deps/deno.land/x/sha1@v1.0.3/mod.js";
2
+ /**
3
+ * Creates a new script.
4
+ *
5
+ * Scripts offer the ability to optimistically try to execute a script without having to send the
6
+ * entire script to the server. If the script is loaded on the server, it tries again by sending
7
+ * the entire script. Afterwards, the script is cached on the server.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const redis = new Redis({...})
12
+ *
13
+ * const script = redis.createScript<string>("return ARGV[1];")
14
+ * const arg1 = await script.eval([], ["Hello World"])
15
+ * assertEquals(arg1, "Hello World")
16
+ * ```
17
+ */
18
+ export class Script {
19
+ constructor(redis, script) {
20
+ Object.defineProperty(this, "script", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "sha1", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "redis", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ this.redis = redis;
39
+ this.sha1 = this.digest(script);
40
+ this.script = script;
41
+ }
42
+ /**
43
+ * Send an `EVAL` command to redis.
44
+ */
45
+ async eval(keys, args) {
46
+ return await this.redis.eval(this.script, keys, args);
47
+ }
48
+ /**
49
+ * Calculates the sha1 hash of the script and then calls `EVALSHA`.
50
+ */
51
+ async evalsha(keys, args) {
52
+ return await this.redis.evalsha(this.sha1, keys, args);
53
+ }
54
+ /**
55
+ * Optimistically try to run `EVALSHA` first.
56
+ * If the script is not loaded in redis, it will fall back and try again with `EVAL`.
57
+ *
58
+ * Following calls will be able to use the cached script
59
+ */
60
+ async exec(keys, args) {
61
+ const res = await this.redis.evalsha(this.sha1, keys, args).catch(async (err) => {
62
+ if (err instanceof Error &&
63
+ err.message.toLowerCase().includes("noscript")) {
64
+ return await this.redis.eval(this.script, keys, args);
65
+ }
66
+ throw err;
67
+ });
68
+ return res;
69
+ }
70
+ /**
71
+ * Compute the sha1 hash of the script and return its hex representation.
72
+ */
73
+ digest(s) {
74
+ const hash = digest(s, "utf8", "hex");
75
+ return typeof hash === "string" ? hash : new TextDecoder().decode(hash);
76
+ }
77
+ }
@@ -1,5 +1,6 @@
1
1
  import * as core from "../pkg/redis.js";
2
2
  import { HttpClient } from "../pkg/http.js";
3
+ import { VERSION } from "../version.js";
3
4
  /**
4
5
  * Serverless redis client for upstash.
5
6
  */
@@ -15,7 +16,7 @@ export class Redis extends core.Redis {
15
16
  * });
16
17
  * ```
17
18
  */
18
- constructor(config) {
19
+ constructor(config, env) {
19
20
  if (config.url.startsWith(" ") ||
20
21
  config.url.endsWith(" ") ||
21
22
  /\r|\n/.test(config.url)) {
@@ -26,10 +27,17 @@ export class Redis extends core.Redis {
26
27
  /\r|\n/.test(config.token)) {
27
28
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
28
29
  }
30
+ const telemetry = {};
31
+ if (!env?.UPSTASH_DISABLE_TELEMETRY) {
32
+ telemetry.platform = "cloudflare";
33
+ telemetry.sdk = `@upstash/redis@${VERSION}`;
34
+ }
29
35
  const client = new HttpClient({
30
36
  retry: config.retry,
31
37
  baseUrl: config.url,
32
38
  headers: { authorization: `Bearer ${config.token}` },
39
+ responseEncoding: config.responseEncoding,
40
+ telemetry,
33
41
  });
34
42
  super(client, {
35
43
  automaticDeserialization: config.automaticDeserialization,
@@ -57,6 +65,6 @@ export class Redis extends core.Redis {
57
65
  if (!token) {
58
66
  throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`");
59
67
  }
60
- return new Redis({ ...opts, url, token });
68
+ return new Redis({ ...opts, url, token }, env);
61
69
  }
62
70
  }
@@ -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,
@@ -1,7 +1,21 @@
1
1
  // deno-lint-ignore-file
2
2
  import * as core from "../pkg/redis.js";
3
3
  import { HttpClient, } from "../pkg/http.js";
4
+ import { VERSION } from "../version.js";
4
5
  import "isomorphic-fetch";
6
+ // @ts-ignore Deno can't compile
7
+ // import https from "https";
8
+ // @ts-ignore Deno can't compile
9
+ // import http from "http";
10
+ // import "isomorphic-fetch";
11
+ /**
12
+ * Workaround for nodejs 14, where atob is not included in the standardlib
13
+ */
14
+ if (typeof atob === "undefined") {
15
+ global.atob = function (b64) {
16
+ return Buffer.from(b64, "base64").toString("utf-8");
17
+ };
18
+ }
5
19
  /**
6
20
  * Serverless redis client for upstash.
7
21
  */
@@ -21,11 +35,23 @@ export class Redis extends core.Redis {
21
35
  /\r|\n/.test(configOrRequester.token)) {
22
36
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
23
37
  }
38
+ const telemetry = {};
39
+ if (!process.env.UPSTASH_DISABLE_TELEMETRY) {
40
+ telemetry.runtime = `node@${process.version}`;
41
+ telemetry.platform = process.env.VERCEL
42
+ ? "vercel"
43
+ : process.env.AWS_REGION
44
+ ? "aws"
45
+ : "unknown";
46
+ telemetry.sdk = `@upstash/redis@${VERSION}`;
47
+ }
24
48
  const client = new HttpClient({
25
49
  baseUrl: configOrRequester.url,
26
50
  retry: configOrRequester.retry,
27
51
  headers: { authorization: `Bearer ${configOrRequester.token}` },
28
- // agent: configOrRequester.agent,
52
+ // agent: configOrRequester.agent,
53
+ responseEncoding: configOrRequester.responseEncoding,
54
+ telemetry,
29
55
  });
30
56
  super(client, {
31
57
  automaticDeserialization: configOrRequester.automaticDeserialization,