@upstash/redis 0.0.0-ci.ba39ba1e → 0.0.0-ci.bc3aba1f-20221124

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 (80) hide show
  1. package/README.md +14 -19
  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/lpos.js +19 -0
  11. package/esm/pkg/commands/mod.js +3 -0
  12. package/esm/pkg/commands/scan.js +3 -0
  13. package/esm/pkg/commands/sdiffstore.js +1 -1
  14. package/esm/pkg/commands/set.js +16 -4
  15. package/esm/pkg/commands/zmscore.js +10 -0
  16. package/esm/pkg/commands/zrange.js +6 -0
  17. package/esm/pkg/http.js +82 -2
  18. package/esm/pkg/pipeline.js +52 -6
  19. package/esm/pkg/redis.js +79 -2
  20. package/esm/pkg/script.js +77 -0
  21. package/esm/platforms/cloudflare.js +10 -2
  22. package/esm/platforms/fastly.js +1 -0
  23. package/esm/platforms/node_with_fetch.js +14 -1
  24. package/esm/platforms/nodejs.js +14 -1
  25. package/esm/version.js +1 -0
  26. package/package.json +1 -39
  27. package/script/deps/deno.land/x/base64@v0.2.1/base.js +104 -0
  28. package/script/deps/deno.land/x/base64@v0.2.1/base64url.js +13 -0
  29. package/script/deps/deno.land/x/sha1@v1.0.3/deps.js +6 -0
  30. package/script/deps/deno.land/x/sha1@v1.0.3/mod.js +196 -0
  31. package/script/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +55 -0
  32. package/script/pkg/commands/getdel.js +13 -0
  33. package/script/pkg/commands/hgetall.js +0 -4
  34. package/script/pkg/commands/hrandfield.js +43 -0
  35. package/script/pkg/commands/lpos.js +23 -0
  36. package/script/pkg/commands/mod.js +3 -0
  37. package/script/pkg/commands/scan.js +3 -0
  38. package/script/pkg/commands/sdiffstore.js +1 -1
  39. package/script/pkg/commands/set.js +16 -4
  40. package/script/pkg/commands/zmscore.js +14 -0
  41. package/script/pkg/commands/zrange.js +6 -0
  42. package/script/pkg/http.js +82 -2
  43. package/script/pkg/pipeline.js +51 -5
  44. package/script/pkg/redis.js +78 -1
  45. package/script/pkg/script.js +81 -0
  46. package/script/platforms/cloudflare.js +10 -2
  47. package/script/platforms/fastly.js +1 -0
  48. package/script/platforms/node_with_fetch.js +14 -1
  49. package/script/platforms/nodejs.js +14 -1
  50. package/script/version.js +4 -0
  51. package/types/deps/deno.land/x/base64@v0.2.1/base.d.ts +5 -0
  52. package/types/deps/deno.land/x/base64@v0.2.1/base64url.d.ts +1 -0
  53. package/types/deps/deno.land/x/sha1@v1.0.3/deps.d.ts +1 -0
  54. package/types/deps/deno.land/x/sha1@v1.0.3/mod.d.ts +26 -0
  55. package/types/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.d.ts +3 -0
  56. package/types/pkg/commands/bitop.d.ts +0 -1
  57. package/types/pkg/commands/bitpos.d.ts +1 -1
  58. package/types/pkg/commands/getdel.d.ts +7 -0
  59. package/types/pkg/commands/hrandfield.d.ts +9 -0
  60. package/types/pkg/commands/lpop.d.ts +1 -1
  61. package/types/pkg/commands/lpos.d.ts +15 -0
  62. package/types/pkg/commands/mget.d.ts +1 -1
  63. package/types/pkg/commands/mod.d.ts +3 -0
  64. package/types/pkg/commands/rpop.d.ts +2 -2
  65. package/types/pkg/commands/scan.d.ts +1 -0
  66. package/types/pkg/commands/sdiffstore.d.ts +1 -1
  67. package/types/pkg/commands/set.d.ts +31 -2
  68. package/types/pkg/commands/spop.d.ts +2 -2
  69. package/types/pkg/commands/zadd.d.ts +1 -1
  70. package/types/pkg/commands/zmscore.d.ts +7 -0
  71. package/types/pkg/commands/zrange.d.ts +7 -0
  72. package/types/pkg/http.d.ts +52 -4
  73. package/types/pkg/pipeline.d.ts +33 -8
  74. package/types/pkg/redis.d.ts +48 -8
  75. package/types/pkg/script.d.ts +42 -0
  76. package/types/platforms/cloudflare.d.ts +8 -7
  77. package/types/platforms/fastly.d.ts +2 -6
  78. package/types/platforms/node_with_fetch.d.ts +2 -21
  79. package/types/platforms/nodejs.d.ts +3 -6
  80. 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,
@@ -48,6 +67,7 @@ export class HttpClient {
48
67
  headers: this.headers,
49
68
  body: JSON.stringify(req.body),
50
69
  keepalive: true,
70
+ agent: this.options?.agent,
51
71
  /**
52
72
  * Fastly specific
53
73
  */
@@ -72,6 +92,66 @@ export class HttpClient {
72
92
  if (!res.ok) {
73
93
  throw new UpstashError(body.error);
74
94
  }
95
+ if (this.options?.responseEncoding === "base64") {
96
+ return Array.isArray(body) ? body.map(decode) : decode(body);
97
+ }
75
98
  return body;
76
99
  }
77
100
  }
101
+ function base64decode(b64) {
102
+ let dec = "";
103
+ try {
104
+ /**
105
+ * Using only atob() is not enough because it doesn't work with unicode characters
106
+ */
107
+ const binString = atob(b64);
108
+ const size = binString.length;
109
+ const bytes = new Uint8Array(size);
110
+ for (let i = 0; i < size; i++) {
111
+ bytes[i] = binString.charCodeAt(i);
112
+ }
113
+ dec = new TextDecoder().decode(bytes);
114
+ }
115
+ catch {
116
+ dec = b64;
117
+ }
118
+ try {
119
+ return decodeURIComponent(dec);
120
+ }
121
+ catch {
122
+ return dec;
123
+ }
124
+ }
125
+ function decode(raw) {
126
+ let result = undefined;
127
+ switch (typeof raw.result) {
128
+ case "undefined":
129
+ return raw;
130
+ case "number": {
131
+ result = raw.result;
132
+ break;
133
+ }
134
+ case "object": {
135
+ if (Array.isArray(raw.result)) {
136
+ result = raw.result.map((v) => typeof v === "string"
137
+ ? base64decode(v)
138
+ : Array.isArray(v)
139
+ ? v.map(base64decode)
140
+ : v);
141
+ }
142
+ else {
143
+ // If it's not an array it must be null
144
+ // Apparently null is an object in javascript
145
+ result = null;
146
+ }
147
+ break;
148
+ }
149
+ case "string": {
150
+ result = raw.result === "OK" ? "OK" : base64decode(raw.result);
151
+ break;
152
+ }
153
+ default:
154
+ break;
155
+ }
156
+ return { result, error: raw.error };
157
+ }
@@ -1,5 +1,7 @@
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, 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, 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";
3
5
  /**
4
6
  * Upstash REST API supports command pipelining to send multiple commands in
5
7
  * batch, instead of sending each command one by one and waiting for a response.
@@ -16,7 +18,7 @@ import { UpstashError } from "./error.js";
16
18
  * **Examples:**
17
19
  *
18
20
  * ```ts
19
- * const p = redis.pipeline()
21
+ * const p = redis.pipeline() // or redis.multi()
20
22
  * p.set("key","value")
21
23
  * p.get("key")
22
24
  * const res = await p.exec()
@@ -39,7 +41,7 @@ import { UpstashError } from "./error.js";
39
41
  * ```
40
42
  */
41
43
  export class Pipeline {
42
- constructor(client, commandOptions) {
44
+ constructor(opts) {
43
45
  Object.defineProperty(this, "client", {
44
46
  enumerable: true,
45
47
  configurable: true,
@@ -58,6 +60,12 @@ export class Pipeline {
58
60
  writable: true,
59
61
  value: void 0
60
62
  });
63
+ Object.defineProperty(this, "multiExec", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: void 0
68
+ });
61
69
  /**
62
70
  * Send the pipeline request to upstash.
63
71
  *
@@ -76,8 +84,9 @@ export class Pipeline {
76
84
  if (this.commands.length === 0) {
77
85
  throw new Error("Pipeline is empty");
78
86
  }
87
+ const path = this.multiExec ? ["multi-exec"] : ["pipeline"];
79
88
  const res = (await this.client.request({
80
- path: ["pipeline"],
89
+ path,
81
90
  body: Object.values(this.commands).map((c) => c.command),
82
91
  }));
83
92
  return res.map(({ error, result }, i) => {
@@ -250,6 +259,15 @@ export class Pipeline {
250
259
  writable: true,
251
260
  value: (...args) => this.chain(new GetBitCommand(args, this.commandOptions))
252
261
  });
262
+ /**
263
+ * @see https://redis.io/commands/getdel
264
+ */
265
+ Object.defineProperty(this, "getdel", {
266
+ enumerable: true,
267
+ configurable: true,
268
+ writable: true,
269
+ value: (...args) => this.chain(new GetDelCommand(args, this.commandOptions))
270
+ });
253
271
  /**
254
272
  * @see https://redis.io/commands/getrange
255
273
  */
@@ -358,6 +376,15 @@ export class Pipeline {
358
376
  writable: true,
359
377
  value: (key, kv) => this.chain(new HMSetCommand([key, kv], this.commandOptions))
360
378
  });
379
+ /**
380
+ * @see https://redis.io/commands/hrandfield
381
+ */
382
+ Object.defineProperty(this, "hrandfield", {
383
+ enumerable: true,
384
+ configurable: true,
385
+ writable: true,
386
+ value: (key, count, withValues) => this.chain(new HRandFieldCommand([key, count, withValues], this.commandOptions))
387
+ });
361
388
  /**
362
389
  * @see https://redis.io/commands/hscan
363
390
  */
@@ -475,6 +502,15 @@ export class Pipeline {
475
502
  writable: true,
476
503
  value: (...args) => this.chain(new LPopCommand(args, this.commandOptions))
477
504
  });
505
+ /**
506
+ * @see https://redis.io/commands/lpos
507
+ */
508
+ Object.defineProperty(this, "lpos", {
509
+ enumerable: true,
510
+ configurable: true,
511
+ writable: true,
512
+ value: (...args) => this.chain(new LPosCommand(args, this.commandOptions))
513
+ });
478
514
  /**
479
515
  * @see https://redis.io/commands/lpush
480
516
  */
@@ -1002,6 +1038,15 @@ export class Pipeline {
1002
1038
  writable: true,
1003
1039
  value: (...args) => this.chain(new ZLexCountCommand(args, this.commandOptions))
1004
1040
  });
1041
+ /**
1042
+ * @see https://redis.io/commands/zmscore
1043
+ */
1044
+ Object.defineProperty(this, "zmscore", {
1045
+ enumerable: true,
1046
+ configurable: true,
1047
+ writable: true,
1048
+ value: (...args) => this.chain(new ZMScoreCommand(args, this.commandOptions))
1049
+ });
1005
1050
  /**
1006
1051
  * @see https://redis.io/commands/zpopmax
1007
1052
  */
@@ -1110,9 +1155,10 @@ export class Pipeline {
1110
1155
  writable: true,
1111
1156
  value: (...args) => this.chain(new ZUnionStoreCommand(args, this.commandOptions))
1112
1157
  });
1113
- this.client = client;
1158
+ this.client = opts.client;
1114
1159
  this.commands = [];
1115
- this.commandOptions = commandOptions;
1160
+ this.commandOptions = opts.commandOptions;
1161
+ this.multiExec = opts.multiExec ?? false;
1116
1162
  }
1117
1163
  /**
1118
1164
  * Pushes a command into the pipelien and returns a chainable instance of the
package/esm/pkg/redis.js CHANGED
@@ -1,5 +1,7 @@
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, 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, 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";
3
5
  /**
4
6
  * Serverless redis client for upstash.
5
7
  */
@@ -28,6 +30,18 @@ export class Redis {
28
30
  writable: true,
29
31
  value: void 0
30
32
  });
33
+ /**
34
+ * Wrap a new middleware around the HTTP client.
35
+ */
36
+ Object.defineProperty(this, "use", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: (middleware) => {
41
+ const makeRequest = this.client.request.bind(this.client);
42
+ this.client.request = (req) => middleware(req, makeRequest);
43
+ }
44
+ });
31
45
  /**
32
46
  * Create a new pipeline that allows you to send requests in bulk.
33
47
  *
@@ -37,7 +51,30 @@ export class Redis {
37
51
  enumerable: true,
38
52
  configurable: true,
39
53
  writable: true,
40
- value: () => new Pipeline(this.client, this.opts)
54
+ value: () => new Pipeline({
55
+ client: this.client,
56
+ commandOptions: this.opts,
57
+ multiExec: false,
58
+ })
59
+ });
60
+ /**
61
+ * Create a new transaction to allow executing multiple steps atomically.
62
+ *
63
+ * All the commands in a transaction are serialized and executed sequentially. A request sent by
64
+ * another client will never be served in the middle of the execution of a Redis Transaction. This
65
+ * guarantees that the commands are executed as a single isolated operation.
66
+ *
67
+ * @see {@link Pipeline}
68
+ */
69
+ Object.defineProperty(this, "multi", {
70
+ enumerable: true,
71
+ configurable: true,
72
+ writable: true,
73
+ value: () => new Pipeline({
74
+ client: this.client,
75
+ commandOptions: this.opts,
76
+ multiExec: true,
77
+ })
41
78
  });
42
79
  /**
43
80
  * @see https://redis.io/commands/append
@@ -201,6 +238,15 @@ export class Redis {
201
238
  writable: true,
202
239
  value: (...args) => new GetBitCommand(args, this.opts).exec(this.client)
203
240
  });
241
+ /**
242
+ * @see https://redis.io/commands/getdel
243
+ */
244
+ Object.defineProperty(this, "getdel", {
245
+ enumerable: true,
246
+ configurable: true,
247
+ writable: true,
248
+ value: (...args) => new GetDelCommand(args, this.opts).exec(this.client)
249
+ });
204
250
  /**
205
251
  * @see https://redis.io/commands/getrange
206
252
  */
@@ -309,6 +355,16 @@ export class Redis {
309
355
  writable: true,
310
356
  value: (key, kv) => new HMSetCommand([key, kv], this.opts).exec(this.client)
311
357
  });
358
+ /**
359
+ * @see https://redis.io/commands/hrandfield
360
+ */
361
+ Object.defineProperty(this, "hrandfield", {
362
+ enumerable: true,
363
+ configurable: true,
364
+ writable: true,
365
+ value: (key, count, withValues) => new HRandFieldCommand([key, count, withValues], this.opts)
366
+ .exec(this.client)
367
+ });
312
368
  /**
313
369
  * @see https://redis.io/commands/hscan
314
370
  */
@@ -426,6 +482,15 @@ export class Redis {
426
482
  writable: true,
427
483
  value: (...args) => new LPopCommand(args, this.opts).exec(this.client)
428
484
  });
485
+ /**
486
+ * @see https://redis.io/commands/lpos
487
+ */
488
+ Object.defineProperty(this, "lpos", {
489
+ enumerable: true,
490
+ configurable: true,
491
+ writable: true,
492
+ value: (...args) => new LPosCommand(args, this.opts).exec(this.client)
493
+ });
429
494
  /**
430
495
  * @see https://redis.io/commands/lpush
431
496
  */
@@ -953,6 +1018,15 @@ export class Redis {
953
1018
  writable: true,
954
1019
  value: (...args) => new ZLexCountCommand(args, this.opts).exec(this.client)
955
1020
  });
1021
+ /**
1022
+ * @see https://redis.io/commands/zmscore
1023
+ */
1024
+ Object.defineProperty(this, "zmscore", {
1025
+ enumerable: true,
1026
+ configurable: true,
1027
+ writable: true,
1028
+ value: (...args) => new ZMScoreCommand(args, this.opts).exec(this.client)
1029
+ });
956
1030
  /**
957
1031
  * @see https://redis.io/commands/zpopmax
958
1032
  */
@@ -1064,4 +1138,7 @@ export class Redis {
1064
1138
  this.client = client;
1065
1139
  this.opts = opts;
1066
1140
  }
1141
+ createScript(script) {
1142
+ return new Script(this, script);
1143
+ }
1067
1144
  }
@@ -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,6 +1,7 @@
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";
5
6
  /**
6
7
  * Serverless redis client for upstash.
@@ -21,11 +22,23 @@ export class Redis extends core.Redis {
21
22
  /\r|\n/.test(configOrRequester.token)) {
22
23
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
23
24
  }
25
+ const telemetry = {};
26
+ if (!process.env.UPSTASH_DISABLE_TELEMETRY) {
27
+ telemetry.runtime = `node@${process.version}`;
28
+ telemetry.platform = process.env.VERCEL
29
+ ? "vercel"
30
+ : process.env.AWS_REGION
31
+ ? "aws"
32
+ : "unknown";
33
+ telemetry.sdk = `@upstash/redis@${VERSION}`;
34
+ }
24
35
  const client = new HttpClient({
25
36
  baseUrl: configOrRequester.url,
26
37
  retry: configOrRequester.retry,
27
38
  headers: { authorization: `Bearer ${configOrRequester.token}` },
28
- // agent: configOrRequester.agent,
39
+ // agent: configOrRequester.agent,
40
+ responseEncoding: configOrRequester.responseEncoding,
41
+ telemetry,
29
42
  });
30
43
  super(client, {
31
44
  automaticDeserialization: configOrRequester.automaticDeserialization,
@@ -1,6 +1,7 @@
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
  /**
5
6
  * Serverless redis client for upstash.
6
7
  */
@@ -20,11 +21,23 @@ export class Redis extends core.Redis {
20
21
  /\r|\n/.test(configOrRequester.token)) {
21
22
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
22
23
  }
24
+ const telemetry = {};
25
+ if (!process.env.UPSTASH_DISABLE_TELEMETRY) {
26
+ telemetry.runtime = `node@${process.version}`;
27
+ telemetry.platform = process.env.VERCEL
28
+ ? "vercel"
29
+ : process.env.AWS_REGION
30
+ ? "aws"
31
+ : "unknown";
32
+ telemetry.sdk = `@upstash/redis@${VERSION}`;
33
+ }
23
34
  const client = new HttpClient({
24
35
  baseUrl: configOrRequester.url,
25
36
  retry: configOrRequester.retry,
26
37
  headers: { authorization: `Bearer ${configOrRequester.token}` },
27
- // agent: configOrRequester.agent,
38
+ agent: configOrRequester.agent,
39
+ responseEncoding: configOrRequester.responseEncoding,
40
+ telemetry,
28
41
  });
29
42
  super(client, {
30
43
  automaticDeserialization: configOrRequester.automaticDeserialization,
package/esm/version.js ADDED
@@ -0,0 +1 @@
1
+ export const VERSION = "v0.0.0-ci.bc3aba1f-20221124";
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": "v0.0.0-ci.ba39ba1e",
6
+ "version": "v0.0.0-ci.bc3aba1f-20221124",
7
7
  "description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
8
8
  "repository": {
9
9
  "type": "git",
@@ -22,10 +22,6 @@
22
22
  "url": "https://github.com/upstash/upstash-redis/issues"
23
23
  },
24
24
  "homepage": "https://github.com/upstash/upstash-redis#readme",
25
- "devDependencies": {
26
- "@size-limit/preset-small-lib": "latest",
27
- "size-limit": "latest"
28
- },
29
25
  "dependencies": {
30
26
  "isomorphic-fetch": "^3.0.0"
31
27
  },
@@ -37,40 +33,6 @@
37
33
  "with-fetch": "./types/platforms/node_with_fetch.d.ts"
38
34
  }
39
35
  },
40
- "size-limit": [
41
- {
42
- "path": "esm/platforms/nodejs.js",
43
- "limit": "6 KB"
44
- },
45
- {
46
- "path": "esm/platforms/fastly.js",
47
- "limit": "6 KB"
48
- },
49
- {
50
- "path": "esm/platforms/cloudflare.js",
51
- "limit": "6 KB"
52
- },
53
- {
54
- "path": "esm/platforms/node_with_fetch.js",
55
- "limit": "15 KB"
56
- },
57
- {
58
- "path": "script/platforms/nodejs.js",
59
- "limit": "10 KB"
60
- },
61
- {
62
- "path": "script/platforms/fastly.js",
63
- "limit": "10 KB"
64
- },
65
- {
66
- "path": "script/platforms/cloudflare.js",
67
- "limit": "10 KB"
68
- },
69
- {
70
- "path": "script/platforms/node_with_fetch.js",
71
- "limit": "15 KB"
72
- }
73
- ],
74
36
  "exports": {
75
37
  ".": {
76
38
  "import": "./esm/platforms/nodejs.js",