@upstash/redis 0.0.0-ci.f55d85c5 → 0.0.0-ci.fb6b986f-20221006

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 (74) hide show
  1. package/README.md +36 -274
  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/lpos.js +19 -0
  8. package/esm/pkg/commands/mod.js +1 -0
  9. package/esm/pkg/commands/scan.js +3 -0
  10. package/esm/pkg/commands/script_exists.js +1 -10
  11. package/esm/pkg/commands/sdiffstore.js +1 -1
  12. package/esm/pkg/commands/set.js +16 -4
  13. package/esm/pkg/commands/zmscore.js +10 -0
  14. package/esm/pkg/commands/zrange.js +6 -0
  15. package/esm/pkg/http.js +88 -9
  16. package/esm/pkg/pipeline.js +20 -1
  17. package/esm/pkg/redis.js +36 -1
  18. package/esm/pkg/script.js +77 -0
  19. package/esm/platforms/cloudflare.js +5 -25
  20. package/esm/platforms/fastly.js +4 -25
  21. package/esm/platforms/node_with_fetch.js +4 -25
  22. package/esm/platforms/nodejs.js +4 -25
  23. package/package.json +10 -37
  24. package/script/deps/deno.land/x/base64@v0.2.1/base.js +104 -0
  25. package/script/deps/deno.land/x/base64@v0.2.1/base64url.js +13 -0
  26. package/script/deps/deno.land/x/sha1@v1.0.3/deps.js +6 -0
  27. package/script/deps/deno.land/x/sha1@v1.0.3/mod.js +196 -0
  28. package/script/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +55 -0
  29. package/script/pkg/commands/lpos.js +23 -0
  30. package/script/pkg/commands/mod.js +1 -0
  31. package/script/pkg/commands/scan.js +3 -0
  32. package/script/pkg/commands/script_exists.js +1 -10
  33. package/script/pkg/commands/sdiffstore.js +1 -1
  34. package/script/pkg/commands/set.js +16 -4
  35. package/script/pkg/commands/zmscore.js +14 -0
  36. package/script/pkg/commands/zrange.js +6 -0
  37. package/script/pkg/http.js +88 -9
  38. package/script/pkg/pipeline.js +19 -0
  39. package/script/pkg/redis.js +35 -0
  40. package/script/pkg/script.js +81 -0
  41. package/script/platforms/cloudflare.js +5 -25
  42. package/script/platforms/fastly.js +4 -25
  43. package/script/platforms/node_with_fetch.js +4 -25
  44. package/script/platforms/nodejs.js +4 -25
  45. package/types/deps/deno.land/x/base64@v0.2.1/base.d.ts +5 -0
  46. package/types/deps/deno.land/x/base64@v0.2.1/base64url.d.ts +1 -0
  47. package/types/deps/deno.land/x/sha1@v1.0.3/deps.d.ts +1 -0
  48. package/types/deps/deno.land/x/sha1@v1.0.3/mod.d.ts +26 -0
  49. package/types/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.d.ts +3 -0
  50. package/types/pkg/commands/bitop.d.ts +0 -1
  51. package/types/pkg/commands/bitpos.d.ts +1 -1
  52. package/types/pkg/commands/hmset.d.ts +2 -2
  53. package/types/pkg/commands/lpop.d.ts +1 -1
  54. package/types/pkg/commands/lpos.d.ts +15 -0
  55. package/types/pkg/commands/mget.d.ts +1 -1
  56. package/types/pkg/commands/mod.d.ts +1 -0
  57. package/types/pkg/commands/rpop.d.ts +2 -2
  58. package/types/pkg/commands/scan.d.ts +1 -0
  59. package/types/pkg/commands/script_exists.d.ts +2 -4
  60. package/types/pkg/commands/sdiffstore.d.ts +1 -1
  61. package/types/pkg/commands/set.d.ts +31 -2
  62. package/types/pkg/commands/sinterstore.d.ts +2 -2
  63. package/types/pkg/commands/spop.d.ts +2 -2
  64. package/types/pkg/commands/zadd.d.ts +1 -1
  65. package/types/pkg/commands/zmscore.d.ts +7 -0
  66. package/types/pkg/commands/zrange.d.ts +7 -0
  67. package/types/pkg/http.d.ts +27 -3
  68. package/types/pkg/pipeline.d.ts +19 -7
  69. package/types/pkg/redis.d.ts +29 -11
  70. package/types/pkg/script.d.ts +42 -0
  71. package/types/platforms/cloudflare.d.ts +6 -2
  72. package/types/platforms/fastly.d.ts +5 -1
  73. package/types/platforms/node_with_fetch.d.ts +20 -1
  74. package/types/platforms/nodejs.d.ts +20 -1
@@ -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,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
+ }
@@ -42,6 +42,7 @@ export * from "./lindex.js";
42
42
  export * from "./linsert.js";
43
43
  export * from "./llen.js";
44
44
  export * from "./lpop.js";
45
+ export * from "./lpos.js";
45
46
  export * from "./lpush.js";
46
47
  export * from "./lpushx.js";
47
48
  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,14 +19,34 @@ 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
+ });
22
28
  this.baseUrl = config.baseUrl.replace(/\/$/, "");
23
- this.headers = { "Content-Type": "application/json", ...config.headers };
24
- this.options = config.options;
29
+ this.headers = {
30
+ "Content-Type": "application/json",
31
+ "Upstash-Encoding": "base64",
32
+ ...config.headers,
33
+ };
34
+ this.options = { backend: config.options?.backend };
35
+ if (typeof config?.retry === "boolean" && config?.retry === false) {
36
+ this.retry = {
37
+ attempts: 1,
38
+ backoff: () => 0,
39
+ };
40
+ }
41
+ else {
42
+ this.retry = {
43
+ attempts: config?.retry?.retries ?? 5,
44
+ backoff: config?.retry?.backoff ??
45
+ ((retryCount) => Math.exp(retryCount) * 50),
46
+ };
47
+ }
25
48
  }
26
49
  async request(req) {
27
- if (!req.path) {
28
- req.path = [];
29
- }
30
50
  const requestOptions = {
31
51
  method: "POST",
32
52
  headers: this.headers,
@@ -37,13 +57,72 @@ export class HttpClient {
37
57
  */
38
58
  backend: this.options?.backend,
39
59
  };
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);
60
+ let res = null;
61
+ let error = null;
62
+ for (let i = 0; i <= this.retry.attempts; i++) {
63
+ try {
64
+ res = await fetch([this.baseUrl, ...(req.path ?? [])].join("/"), requestOptions);
65
+ break;
66
+ }
67
+ catch (err) {
68
+ error = err;
69
+ await new Promise((r) => setTimeout(r, this.retry.backoff(i)));
70
+ }
71
+ }
72
+ if (!res) {
73
+ throw error ?? new Error("Exhausted all retries");
74
+ }
43
75
  const body = (await res.json());
44
76
  if (!res.ok) {
45
77
  throw new UpstashError(body.error);
46
78
  }
47
- return body;
79
+ return Array.isArray(body) ? body.map(decode) : decode(body);
80
+ }
81
+ }
82
+ function base64decode(b64) {
83
+ let dec = "";
84
+ try {
85
+ dec = atob(b64).split("").map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2)).join("");
86
+ }
87
+ catch (e) {
88
+ console.warn(`Unable to decode base64 [${dec}]: ${e.message}`);
89
+ return dec;
90
+ }
91
+ try {
92
+ return decodeURIComponent(dec);
93
+ }
94
+ catch (e) {
95
+ console.warn(`Unable to decode URI [${dec}]: ${e.message}`);
96
+ return dec;
97
+ }
98
+ }
99
+ function decode(raw) {
100
+ let result = undefined;
101
+ switch (typeof raw.result) {
102
+ case "undefined":
103
+ return raw;
104
+ case "number":
105
+ result = raw.result;
106
+ break;
107
+ case "object":
108
+ if (Array.isArray(raw.result)) {
109
+ result = raw.result.map((v) => typeof v === "string"
110
+ ? base64decode(v)
111
+ : Array.isArray(v)
112
+ ? v.map(base64decode)
113
+ : v);
114
+ }
115
+ else {
116
+ // If it's not an array it must be null
117
+ // Apparently null is an object in javascript
118
+ result = null;
119
+ }
120
+ break;
121
+ case "string":
122
+ result = raw.result === "OK" ? "OK" : base64decode(raw.result);
123
+ break;
124
+ default:
125
+ break;
48
126
  }
127
+ return { result, error: raw.error };
49
128
  }
@@ -1,5 +1,6 @@
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, 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";
3
4
  /**
4
5
  * Upstash REST API supports command pipelining to send multiple commands in
5
6
  * batch, instead of sending each command one by one and waiting for a response.
@@ -475,6 +476,15 @@ export class Pipeline {
475
476
  writable: true,
476
477
  value: (...args) => this.chain(new LPopCommand(args, this.commandOptions))
477
478
  });
479
+ /**
480
+ * @see https://redis.io/commands/lpos
481
+ */
482
+ Object.defineProperty(this, "lpos", {
483
+ enumerable: true,
484
+ configurable: true,
485
+ writable: true,
486
+ value: (...args) => this.chain(new LPosCommand(args, this.commandOptions))
487
+ });
478
488
  /**
479
489
  * @see https://redis.io/commands/lpush
480
490
  */
@@ -1002,6 +1012,15 @@ export class Pipeline {
1002
1012
  writable: true,
1003
1013
  value: (...args) => this.chain(new ZLexCountCommand(args, this.commandOptions))
1004
1014
  });
1015
+ /**
1016
+ * @see https://redis.io/commands/zmscore
1017
+ */
1018
+ Object.defineProperty(this, "zmscore", {
1019
+ enumerable: true,
1020
+ configurable: true,
1021
+ writable: true,
1022
+ value: (...args) => this.chain(new ZMScoreCommand(args, this.commandOptions))
1023
+ });
1005
1024
  /**
1006
1025
  * @see https://redis.io/commands/zpopmax
1007
1026
  */
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, 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 { 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
  *
@@ -426,6 +440,15 @@ export class Redis {
426
440
  writable: true,
427
441
  value: (...args) => new LPopCommand(args, this.opts).exec(this.client)
428
442
  });
443
+ /**
444
+ * @see https://redis.io/commands/lpos
445
+ */
446
+ Object.defineProperty(this, "lpos", {
447
+ enumerable: true,
448
+ configurable: true,
449
+ writable: true,
450
+ value: (...args) => new LPosCommand(args, this.opts).exec(this.client)
451
+ });
429
452
  /**
430
453
  * @see https://redis.io/commands/lpush
431
454
  */
@@ -953,6 +976,15 @@ export class Redis {
953
976
  writable: true,
954
977
  value: (...args) => new ZLexCountCommand(args, this.opts).exec(this.client)
955
978
  });
979
+ /**
980
+ * @see https://redis.io/commands/zmscore
981
+ */
982
+ Object.defineProperty(this, "zmscore", {
983
+ enumerable: true,
984
+ configurable: true,
985
+ writable: true,
986
+ value: (...args) => new ZMScoreCommand(args, this.opts).exec(this.client)
987
+ });
956
988
  /**
957
989
  * @see https://redis.io/commands/zpopmax
958
990
  */
@@ -1064,4 +1096,7 @@ export class Redis {
1064
1096
  this.client = client;
1065
1097
  this.opts = opts;
1066
1098
  }
1099
+ createScript(script) {
1100
+ return new Script(this, script);
1101
+ }
1067
1102
  }
@@ -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,5 @@
1
1
  import * as core from "../pkg/redis.js";
2
- import { UpstashError } from "../pkg/error.js";
2
+ import { HttpClient } from "../pkg/http.js";
3
3
  /**
4
4
  * Serverless redis client for upstash.
5
5
  */
@@ -26,7 +26,8 @@ export class Redis extends core.Redis {
26
26
  /\r|\n/.test(config.token)) {
27
27
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
28
28
  }
29
- const client = defaultRequester({
29
+ const client = new HttpClient({
30
+ retry: config.retry,
30
31
  baseUrl: config.url,
31
32
  headers: { authorization: `Bearer ${config.token}` },
32
33
  });
@@ -44,9 +45,8 @@ export class Redis extends core.Redis {
44
45
  * ```ts
45
46
  * const redis = Redis.fromEnv(env)
46
47
  * ```
47
- *
48
48
  */
49
- static fromEnv(env) {
49
+ static fromEnv(env, opts) {
50
50
  // @ts-ignore These will be defined by cloudflare
51
51
  const url = env?.UPSTASH_REDIS_REST_URL ?? UPSTASH_REDIS_REST_URL;
52
52
  // @ts-ignore These will be defined by cloudflare
@@ -57,26 +57,6 @@ export class Redis extends core.Redis {
57
57
  if (!token) {
58
58
  throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`");
59
59
  }
60
- return new Redis({ url, token });
60
+ return new Redis({ ...opts, url, token });
61
61
  }
62
62
  }
63
- function defaultRequester(config) {
64
- return {
65
- request: async function (req) {
66
- if (!req.path) {
67
- req.path = [];
68
- }
69
- const res = await fetch([config.baseUrl, ...req.path].join("/"), {
70
- method: "POST",
71
- headers: { "Content-Type": "application/json", ...config.headers },
72
- body: JSON.stringify(req.body),
73
- keepalive: true,
74
- });
75
- const body = (await res.json());
76
- if (!res.ok) {
77
- throw new UpstashError(body.error);
78
- }
79
- return body;
80
- },
81
- };
82
- }
@@ -1,5 +1,5 @@
1
1
  import * as core from "../pkg/redis.js";
2
- import { UpstashError } from "../pkg/error.js";
2
+ import { HttpClient } from "../pkg/http.js";
3
3
  /**
4
4
  * Serverless redis client for upstash.
5
5
  */
@@ -27,35 +27,14 @@ export class Redis extends core.Redis {
27
27
  /\r|\n/.test(config.token)) {
28
28
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
29
29
  }
30
- const client = defaultRequester({
30
+ const client = new HttpClient({
31
31
  baseUrl: config.url,
32
+ retry: config.retry,
32
33
  headers: { authorization: `Bearer ${config.token}` },
33
- backend: config.backend,
34
+ options: { backend: config.backend },
34
35
  });
35
36
  super(client, {
36
37
  automaticDeserialization: config.automaticDeserialization,
37
38
  });
38
39
  }
39
40
  }
40
- function defaultRequester(config) {
41
- return {
42
- request: async function (req) {
43
- if (!req.path) {
44
- req.path = [];
45
- }
46
- const res = await fetch([config.baseUrl, ...req.path].join("/"), {
47
- method: "POST",
48
- headers: { "Content-Type": "application/json", ...config.headers },
49
- body: JSON.stringify(req.body),
50
- keepalive: true,
51
- // @ts-expect-error fastly requires `backend`
52
- backend: config.backend,
53
- });
54
- const body = (await res.json());
55
- if (!res.ok) {
56
- throw new UpstashError(body.error);
57
- }
58
- return body;
59
- },
60
- };
61
- }
@@ -1,6 +1,6 @@
1
1
  // deno-lint-ignore-file
2
2
  import * as core from "../pkg/redis.js";
3
- import { UpstashError } from "../pkg/error.js";
3
+ import { HttpClient, } from "../pkg/http.js";
4
4
  import "isomorphic-fetch";
5
5
  /**
6
6
  * Serverless redis client for upstash.
@@ -21,8 +21,9 @@ export class Redis extends core.Redis {
21
21
  /\r|\n/.test(configOrRequester.token)) {
22
22
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
23
23
  }
24
- const client = defaultRequester({
24
+ const client = new HttpClient({
25
25
  baseUrl: configOrRequester.url,
26
+ retry: configOrRequester.retry,
26
27
  headers: { authorization: `Bearer ${configOrRequester.token}` },
27
28
  // agent: configOrRequester.agent,
28
29
  });
@@ -54,28 +55,6 @@ export class Redis extends core.Redis {
54
55
  if (!token) {
55
56
  throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
56
57
  }
57
- return new Redis({ url, token, ...config });
58
+ return new Redis({ ...config, url, token });
58
59
  }
59
60
  }
60
- function defaultRequester(config) {
61
- return {
62
- request: async function (req) {
63
- if (!req.path) {
64
- req.path = [];
65
- }
66
- const res = await fetch([config.baseUrl, ...req.path].join("/"), {
67
- method: "POST",
68
- headers: { "Content-Type": "application/json", ...config.headers },
69
- body: JSON.stringify(req.body),
70
- keepalive: true,
71
- // @ts-ignore
72
- agent: config.agent,
73
- });
74
- const body = (await res.json());
75
- if (!res.ok) {
76
- throw new UpstashError(body.error);
77
- }
78
- return body;
79
- },
80
- };
81
- }