@upstash/redis 0.0.0-ci.5cd8bd7f → 0.0.0-ci.608697de-20221127

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 (85) hide show
  1. package/README.md +38 -281
  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/script_exists.js +1 -10
  14. package/esm/pkg/commands/sdiffstore.js +1 -1
  15. package/esm/pkg/commands/set.js +16 -4
  16. package/esm/pkg/commands/zmscore.js +10 -0
  17. package/esm/pkg/commands/zrange.js +6 -0
  18. package/esm/pkg/http.js +116 -8
  19. package/esm/pkg/pipeline.js +52 -6
  20. package/esm/pkg/redis.js +79 -2
  21. package/esm/pkg/script.js +77 -0
  22. package/esm/platforms/cloudflare.js +14 -26
  23. package/esm/platforms/fastly.js +5 -25
  24. package/esm/platforms/node_with_fetch.js +18 -26
  25. package/esm/platforms/nodejs.js +18 -26
  26. package/esm/version.js +1 -0
  27. package/package.json +10 -37
  28. package/script/deps/deno.land/x/base64@v0.2.1/base.js +104 -0
  29. package/script/deps/deno.land/x/base64@v0.2.1/base64url.js +13 -0
  30. package/script/deps/deno.land/x/sha1@v1.0.3/deps.js +6 -0
  31. package/script/deps/deno.land/x/sha1@v1.0.3/mod.js +196 -0
  32. package/script/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +55 -0
  33. package/script/pkg/commands/getdel.js +13 -0
  34. package/script/pkg/commands/hgetall.js +0 -4
  35. package/script/pkg/commands/hrandfield.js +43 -0
  36. package/script/pkg/commands/lpos.js +23 -0
  37. package/script/pkg/commands/mod.js +3 -0
  38. package/script/pkg/commands/scan.js +3 -0
  39. package/script/pkg/commands/script_exists.js +1 -10
  40. package/script/pkg/commands/sdiffstore.js +1 -1
  41. package/script/pkg/commands/set.js +16 -4
  42. package/script/pkg/commands/zmscore.js +14 -0
  43. package/script/pkg/commands/zrange.js +6 -0
  44. package/script/pkg/http.js +116 -8
  45. package/script/pkg/pipeline.js +51 -5
  46. package/script/pkg/redis.js +78 -1
  47. package/script/pkg/script.js +81 -0
  48. package/script/platforms/cloudflare.js +14 -26
  49. package/script/platforms/fastly.js +5 -25
  50. package/script/platforms/node_with_fetch.js +18 -26
  51. package/script/platforms/nodejs.js +18 -26
  52. package/script/version.js +4 -0
  53. package/types/deps/deno.land/x/base64@v0.2.1/base.d.ts +5 -0
  54. package/types/deps/deno.land/x/base64@v0.2.1/base64url.d.ts +1 -0
  55. package/types/deps/deno.land/x/sha1@v1.0.3/deps.d.ts +1 -0
  56. package/types/deps/deno.land/x/sha1@v1.0.3/mod.d.ts +26 -0
  57. package/types/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.d.ts +3 -0
  58. package/types/pkg/commands/bitop.d.ts +0 -1
  59. package/types/pkg/commands/bitpos.d.ts +1 -1
  60. package/types/pkg/commands/getdel.d.ts +7 -0
  61. package/types/pkg/commands/hmset.d.ts +2 -2
  62. package/types/pkg/commands/hrandfield.d.ts +9 -0
  63. package/types/pkg/commands/lpop.d.ts +1 -1
  64. package/types/pkg/commands/lpos.d.ts +15 -0
  65. package/types/pkg/commands/mget.d.ts +1 -1
  66. package/types/pkg/commands/mod.d.ts +3 -0
  67. package/types/pkg/commands/rpop.d.ts +2 -2
  68. package/types/pkg/commands/scan.d.ts +1 -0
  69. package/types/pkg/commands/script_exists.d.ts +2 -4
  70. package/types/pkg/commands/sdiffstore.d.ts +1 -1
  71. package/types/pkg/commands/set.d.ts +31 -2
  72. package/types/pkg/commands/sinterstore.d.ts +2 -2
  73. package/types/pkg/commands/spop.d.ts +2 -2
  74. package/types/pkg/commands/zadd.d.ts +1 -1
  75. package/types/pkg/commands/zmscore.d.ts +7 -0
  76. package/types/pkg/commands/zrange.d.ts +7 -0
  77. package/types/pkg/http.d.ts +76 -4
  78. package/types/pkg/pipeline.d.ts +34 -9
  79. package/types/pkg/redis.d.ts +51 -11
  80. package/types/pkg/script.d.ts +42 -0
  81. package/types/platforms/cloudflare.d.ts +8 -3
  82. package/types/platforms/fastly.d.ts +2 -2
  83. package/types/platforms/node_with_fetch.d.ts +2 -2
  84. package/types/platforms/nodejs.d.ts +18 -2
  85. package/types/version.d.ts +1 -0
@@ -0,0 +1,50 @@
1
+ import { toUint8Array, fromUint8Array } from "../../../deno.land/x/base64@v0.2.1/base64url.js";
2
+ const decoder = new TextDecoder();
3
+ const encoder = new TextEncoder();
4
+ /** Serializes a Uint8Array to a hexadecimal string. */
5
+ function toHexString(buf) {
6
+ return buf.reduce((hex, byte) => `${hex}${byte < 16 ? "0" : ""}${byte.toString(16)}`, "");
7
+ }
8
+ /** Deserializes a Uint8Array from a hexadecimal string. */
9
+ function fromHexString(hex) {
10
+ const len = hex.length;
11
+ if (len % 2 || !/^[0-9a-fA-F]+$/.test(hex)) {
12
+ throw new TypeError("Invalid hex string.");
13
+ }
14
+ hex = hex.toLowerCase();
15
+ const buf = new Uint8Array(Math.floor(len / 2));
16
+ const end = len / 2;
17
+ for (let i = 0; i < end; ++i) {
18
+ buf[i] = parseInt(hex.substr(i * 2, 2), 16);
19
+ }
20
+ return buf;
21
+ }
22
+ /** Decodes a Uint8Array to utf8-, base64-, or hex-encoded string. */
23
+ export function decode(buf, encoding = "utf8") {
24
+ if (/^utf-?8$/i.test(encoding)) {
25
+ return decoder.decode(buf);
26
+ }
27
+ else if (/^base64$/i.test(encoding)) {
28
+ return fromUint8Array(buf);
29
+ }
30
+ else if (/^hex(?:adecimal)?$/i.test(encoding)) {
31
+ return toHexString(buf);
32
+ }
33
+ else {
34
+ throw new TypeError("Unsupported string encoding.");
35
+ }
36
+ }
37
+ export function encode(str, encoding = "utf8") {
38
+ if (/^utf-?8$/i.test(encoding)) {
39
+ return encoder.encode(str);
40
+ }
41
+ else if (/^base64$/i.test(encoding)) {
42
+ return toUint8Array(str);
43
+ }
44
+ else if (/^hex(?:adecimal)?$/i.test(encoding)) {
45
+ return fromHexString(str);
46
+ }
47
+ else {
48
+ throw new TypeError("Unsupported string encoding.");
49
+ }
50
+ }
@@ -0,0 +1,9 @@
1
+ import { Command } from "./command.js";
2
+ /**
3
+ * @see https://redis.io/commands/getdel
4
+ */
5
+ export class GetDelCommand extends Command {
6
+ constructor(cmd, opts) {
7
+ super(["getdel", ...cmd], opts);
8
+ }
9
+ }
@@ -1,8 +1,4 @@
1
1
  import { Command } from "./command.js";
2
- /**
3
- * @param result De
4
- * @returns
5
- */
6
2
  function deserialize(result) {
7
3
  if (result.length === 0) {
8
4
  return null;
@@ -0,0 +1,39 @@
1
+ import { Command } from "./command.js";
2
+ function deserialize(result) {
3
+ if (result.length === 0) {
4
+ return null;
5
+ }
6
+ const obj = {};
7
+ while (result.length >= 2) {
8
+ const key = result.shift();
9
+ const value = result.shift();
10
+ try {
11
+ obj[key] = JSON.parse(value);
12
+ }
13
+ catch {
14
+ obj[key] = value;
15
+ }
16
+ }
17
+ return obj;
18
+ }
19
+ /**
20
+ * @see https://redis.io/commands/hrandfield
21
+ */
22
+ export class HRandFieldCommand extends Command {
23
+ constructor(cmd, opts) {
24
+ const command = ["hrandfield", cmd[0]];
25
+ if (typeof cmd[1] === "number") {
26
+ command.push(cmd[1]);
27
+ }
28
+ if (cmd[2]) {
29
+ command.push("WITHVALUES");
30
+ }
31
+ super(command, {
32
+ // @ts-ignore TODO:
33
+ deserialize: cmd[2]
34
+ ? (result) => deserialize(result)
35
+ : opts?.deserialize,
36
+ ...opts,
37
+ });
38
+ }
39
+ }
@@ -0,0 +1,19 @@
1
+ import { Command } from "./command.js";
2
+ /**
3
+ * @see https://redis.io/commands/lpos
4
+ */
5
+ export class LPosCommand extends Command {
6
+ constructor(cmd, opts) {
7
+ const args = ["lpos", cmd[0], cmd[1]];
8
+ if (typeof cmd[2]?.rank === "number") {
9
+ args.push("rank", cmd[2].rank);
10
+ }
11
+ if (typeof cmd[2]?.count === "number") {
12
+ args.push("count", cmd[2].count);
13
+ }
14
+ if (typeof cmd[2]?.maxLen === "number") {
15
+ args.push("maxLen", cmd[2].maxLen);
16
+ }
17
+ super(args, opts);
18
+ }
19
+ }
@@ -17,6 +17,7 @@ export * from "./flushall.js";
17
17
  export * from "./flushdb.js";
18
18
  export * from "./get.js";
19
19
  export * from "./getbit.js";
20
+ export * from "./getdel.js";
20
21
  export * from "./getrange.js";
21
22
  export * from "./getset.js";
22
23
  export * from "./hdel.js";
@@ -29,6 +30,7 @@ export * from "./hkeys.js";
29
30
  export * from "./hlen.js";
30
31
  export * from "./hmget.js";
31
32
  export * from "./hmset.js";
33
+ export * from "./hrandfield.js";
32
34
  export * from "./hscan.js";
33
35
  export * from "./hset.js";
34
36
  export * from "./hsetnx.js";
@@ -42,6 +44,7 @@ export * from "./lindex.js";
42
44
  export * from "./linsert.js";
43
45
  export * from "./llen.js";
44
46
  export * from "./lpop.js";
47
+ export * from "./lpos.js";
45
48
  export * from "./lpush.js";
46
49
  export * from "./lpushx.js";
47
50
  export * from "./lrange.js";
@@ -11,6 +11,9 @@ export class ScanCommand extends Command {
11
11
  if (typeof opts?.count === "number") {
12
12
  command.push("count", opts.count);
13
13
  }
14
+ if (opts?.type && opts.type.length > 0) {
15
+ command.push("type", opts.type);
16
+ }
14
17
  super(command, cmdOpts);
15
18
  }
16
19
  }
@@ -5,16 +5,7 @@ import { Command } from "./command.js";
5
5
  export class ScriptExistsCommand extends Command {
6
6
  constructor(hashes, opts) {
7
7
  super(["script", "exists", ...hashes], {
8
- deserialize: (result) => {
9
- /**
10
- * This isn't very pretty but it does the job.
11
- * The user facing api is clean and will return a single `string` if they provided
12
- * a single script hash, and an array of strings of the same length when given an
13
- * array of hashes.
14
- */
15
- const parsed = result;
16
- return parsed.length === 1 ? parsed[0] : parsed;
17
- },
8
+ deserialize: (result) => result,
18
9
  ...opts,
19
10
  });
20
11
  }
@@ -1,6 +1,6 @@
1
1
  import { Command } from "./command.js";
2
2
  /**
3
- * @see https://redis.io/commands/sdiffstpre
3
+ * @see https://redis.io/commands/sdiffstore
4
4
  */
5
5
  export class SDiffStoreCommand extends Command {
6
6
  constructor(cmd, opts) {
@@ -6,17 +6,29 @@ export class SetCommand extends Command {
6
6
  constructor([key, value, opts], cmdOpts) {
7
7
  const command = ["set", key, value];
8
8
  if (opts) {
9
+ if ("nx" in opts && opts.nx) {
10
+ command.push("nx");
11
+ }
12
+ else if ("xx" in opts && opts.xx) {
13
+ command.push("xx");
14
+ }
15
+ if ("get" in opts && opts.get) {
16
+ command.push("get");
17
+ }
9
18
  if ("ex" in opts && typeof opts.ex === "number") {
10
19
  command.push("ex", opts.ex);
11
20
  }
12
21
  else if ("px" in opts && typeof opts.px === "number") {
13
22
  command.push("px", opts.px);
14
23
  }
15
- if ("nx" in opts && opts.nx) {
16
- command.push("nx");
24
+ else if ("exat" in opts && typeof opts.exat === "number") {
25
+ command.push("exat", opts.exat);
17
26
  }
18
- else if ("xx" in opts && opts.xx) {
19
- command.push("xx");
27
+ else if ("pxat" in opts && typeof opts.pxat === "number") {
28
+ command.push("pxat", opts.pxat);
29
+ }
30
+ else if ("keepTtl" in opts && opts.keepTtl) {
31
+ command.push("keepTtl", opts.keepTtl);
20
32
  }
21
33
  }
22
34
  super(command, cmdOpts);
@@ -0,0 +1,10 @@
1
+ import { Command } from "./command.js";
2
+ /**
3
+ * @see https://redis.io/commands/zmscore
4
+ */
5
+ export class ZMScoreCommand extends Command {
6
+ constructor(cmd, opts) {
7
+ const [key, members] = cmd;
8
+ super(["zmscore", key, ...members], opts);
9
+ }
10
+ }
@@ -12,6 +12,12 @@ export class ZRangeCommand extends Command {
12
12
  if (opts?.byLex) {
13
13
  command.push("bylex");
14
14
  }
15
+ if (opts?.rev) {
16
+ command.push("rev");
17
+ }
18
+ if (typeof opts?.count !== "undefined" && typeof opts?.offset !== "undefined") {
19
+ command.push("limit", opts.offset, opts.count);
20
+ }
15
21
  if (opts?.withScores) {
16
22
  command.push("withscores");
17
23
  }
package/esm/pkg/http.js CHANGED
@@ -19,31 +19,139 @@ export class HttpClient {
19
19
  writable: true,
20
20
  value: void 0
21
21
  });
22
+ Object.defineProperty(this, "retry", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ this.options = {
29
+ backend: config.options?.backend,
30
+ agent: config.agent,
31
+ responseEncoding: config.responseEncoding ?? "base64", // default to base64
32
+ };
22
33
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
23
- this.headers = { "Content-Type": "application/json", ...config.headers };
24
- this.options = config.options;
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
+ }
50
+ if (typeof config?.retry === "boolean" && config?.retry === false) {
51
+ this.retry = {
52
+ attempts: 1,
53
+ backoff: () => 0,
54
+ };
55
+ }
56
+ else {
57
+ this.retry = {
58
+ attempts: config?.retry?.retries ?? 5,
59
+ backoff: config?.retry?.backoff ??
60
+ ((retryCount) => Math.exp(retryCount) * 50),
61
+ };
62
+ }
25
63
  }
26
64
  async request(req) {
27
- if (!req.path) {
28
- req.path = [];
29
- }
30
65
  const requestOptions = {
31
66
  method: "POST",
32
67
  headers: this.headers,
33
68
  body: JSON.stringify(req.body),
34
69
  keepalive: true,
70
+ agent: this.options?.agent,
35
71
  /**
36
72
  * Fastly specific
37
73
  */
38
74
  backend: this.options?.backend,
39
75
  };
40
- // fetch is defined by isomorphic fetch
41
- // eslint-disable-next-line no-undef
42
- const res = await fetch([this.baseUrl, ...req.path].join("/"), requestOptions);
76
+ let res = null;
77
+ let error = null;
78
+ for (let i = 0; i <= this.retry.attempts; i++) {
79
+ try {
80
+ res = await fetch([this.baseUrl, ...(req.path ?? [])].join("/"), requestOptions);
81
+ break;
82
+ }
83
+ catch (err) {
84
+ error = err;
85
+ await new Promise((r) => setTimeout(r, this.retry.backoff(i)));
86
+ }
87
+ }
88
+ if (!res) {
89
+ throw error ?? new Error("Exhausted all retries");
90
+ }
43
91
  const body = (await res.json());
44
92
  if (!res.ok) {
45
93
  throw new UpstashError(body.error);
46
94
  }
95
+ if (this.options?.responseEncoding === "base64") {
96
+ return Array.isArray(body) ? body.map(decode) : decode(body);
97
+ }
47
98
  return body;
48
99
  }
49
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
  }