@upstash/redis 0.0.0-ci.b4d4a1cc → 0.0.0-ci.b984c0e9-20220826

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 (66) 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/sdiffstore.js +1 -1
  11. package/esm/pkg/commands/set.js +16 -4
  12. package/esm/pkg/commands/zrange.js +6 -0
  13. package/esm/pkg/http.js +35 -7
  14. package/esm/pkg/pipeline.js +10 -1
  15. package/esm/pkg/redis.js +26 -1
  16. package/esm/pkg/script.js +77 -0
  17. package/esm/platforms/cloudflare.js +5 -25
  18. package/esm/platforms/fastly.js +4 -25
  19. package/esm/platforms/node_with_fetch.js +4 -25
  20. package/esm/platforms/nodejs.js +19 -31
  21. package/package.json +1 -39
  22. package/script/deps/deno.land/x/base64@v0.2.1/base.js +104 -0
  23. package/script/deps/deno.land/x/base64@v0.2.1/base64url.js +13 -0
  24. package/script/deps/deno.land/x/sha1@v1.0.3/deps.js +6 -0
  25. package/script/deps/deno.land/x/sha1@v1.0.3/mod.js +196 -0
  26. package/script/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +55 -0
  27. package/script/pkg/commands/lpos.js +23 -0
  28. package/script/pkg/commands/mod.js +1 -0
  29. package/script/pkg/commands/scan.js +3 -0
  30. package/script/pkg/commands/sdiffstore.js +1 -1
  31. package/script/pkg/commands/set.js +16 -4
  32. package/script/pkg/commands/zrange.js +6 -0
  33. package/script/pkg/http.js +35 -7
  34. package/script/pkg/pipeline.js +9 -0
  35. package/script/pkg/redis.js +25 -0
  36. package/script/pkg/script.js +81 -0
  37. package/script/platforms/cloudflare.js +5 -25
  38. package/script/platforms/fastly.js +4 -25
  39. package/script/platforms/node_with_fetch.js +4 -25
  40. package/script/platforms/nodejs.js +19 -31
  41. package/types/deps/deno.land/x/base64@v0.2.1/base.d.ts +5 -0
  42. package/types/deps/deno.land/x/base64@v0.2.1/base64url.d.ts +1 -0
  43. package/types/deps/deno.land/x/sha1@v1.0.3/deps.d.ts +1 -0
  44. package/types/deps/deno.land/x/sha1@v1.0.3/mod.d.ts +26 -0
  45. package/types/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.d.ts +3 -0
  46. package/types/pkg/commands/bitop.d.ts +0 -1
  47. package/types/pkg/commands/bitpos.d.ts +1 -1
  48. package/types/pkg/commands/lpop.d.ts +1 -1
  49. package/types/pkg/commands/lpos.d.ts +15 -0
  50. package/types/pkg/commands/mget.d.ts +1 -1
  51. package/types/pkg/commands/mod.d.ts +1 -0
  52. package/types/pkg/commands/rpop.d.ts +2 -2
  53. package/types/pkg/commands/scan.d.ts +1 -0
  54. package/types/pkg/commands/sdiffstore.d.ts +1 -1
  55. package/types/pkg/commands/set.d.ts +31 -2
  56. package/types/pkg/commands/spop.d.ts +2 -2
  57. package/types/pkg/commands/zadd.d.ts +1 -1
  58. package/types/pkg/commands/zrange.d.ts +7 -0
  59. package/types/pkg/http.d.ts +27 -3
  60. package/types/pkg/pipeline.d.ts +14 -6
  61. package/types/pkg/redis.d.ts +22 -8
  62. package/types/pkg/script.d.ts +42 -0
  63. package/types/platforms/cloudflare.d.ts +6 -2
  64. package/types/platforms/fastly.d.ts +5 -1
  65. package/types/platforms/node_with_fetch.d.ts +20 -1
  66. 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
  }
@@ -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);
@@ -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,30 @@ 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
29
  this.headers = { "Content-Type": "application/json", ...config.headers };
24
- this.options = config.options;
30
+ this.options = { backend: config.options?.backend };
31
+ if (typeof config?.retry === "boolean" && config?.retry === false) {
32
+ this.retry = {
33
+ attempts: 1,
34
+ backoff: () => 0,
35
+ };
36
+ }
37
+ else {
38
+ this.retry = {
39
+ attempts: config?.retry?.retries ?? 5,
40
+ backoff: config?.retry?.backoff ??
41
+ ((retryCount) => Math.exp(retryCount) * 50),
42
+ };
43
+ }
25
44
  }
26
45
  async request(req) {
27
- if (!req.path) {
28
- req.path = [];
29
- }
30
46
  const requestOptions = {
31
47
  method: "POST",
32
48
  headers: this.headers,
@@ -37,9 +53,21 @@ export class HttpClient {
37
53
  */
38
54
  backend: this.options?.backend,
39
55
  };
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);
56
+ let res = null;
57
+ let error = null;
58
+ for (let i = 0; i <= this.retry.attempts; i++) {
59
+ try {
60
+ res = await fetch([this.baseUrl, ...(req.path ?? [])].join("/"), requestOptions);
61
+ break;
62
+ }
63
+ catch (err) {
64
+ error = err;
65
+ await new Promise((r) => setTimeout(r, this.retry.backoff(i)));
66
+ }
67
+ }
68
+ if (!res) {
69
+ throw error ?? new Error("Exhausted all retries");
70
+ }
43
71
  const body = (await res.json());
44
72
  if (!res.ok) {
45
73
  throw new UpstashError(body.error);
@@ -1,4 +1,4 @@
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
3
  /**
4
4
  * Upstash REST API supports command pipelining to send multiple commands in
@@ -475,6 +475,15 @@ export class Pipeline {
475
475
  writable: true,
476
476
  value: (...args) => this.chain(new LPopCommand(args, this.commandOptions))
477
477
  });
478
+ /**
479
+ * @see https://redis.io/commands/lpos
480
+ */
481
+ Object.defineProperty(this, "lpos", {
482
+ enumerable: true,
483
+ configurable: true,
484
+ writable: true,
485
+ value: (...args) => this.chain(new LPosCommand(args, this.commandOptions))
486
+ });
478
487
  /**
479
488
  * @see https://redis.io/commands/lpush
480
489
  */
package/esm/pkg/redis.js CHANGED
@@ -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 { Pipeline } from "./pipeline.js";
3
+ import { Script } from "./script.js";
3
4
  /**
4
5
  * Serverless redis client for upstash.
5
6
  */
@@ -28,6 +29,18 @@ export class Redis {
28
29
  writable: true,
29
30
  value: void 0
30
31
  });
32
+ /**
33
+ * Wrap a new middleware around the HTTP client.
34
+ */
35
+ Object.defineProperty(this, "use", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: (middleware) => {
40
+ const makeRequest = this.client.request.bind(this.client);
41
+ this.client.request = (req) => middleware(req, makeRequest);
42
+ }
43
+ });
31
44
  /**
32
45
  * Create a new pipeline that allows you to send requests in bulk.
33
46
  *
@@ -426,6 +439,15 @@ export class Redis {
426
439
  writable: true,
427
440
  value: (...args) => new LPopCommand(args, this.opts).exec(this.client)
428
441
  });
442
+ /**
443
+ * @see https://redis.io/commands/lpos
444
+ */
445
+ Object.defineProperty(this, "lpos", {
446
+ enumerable: true,
447
+ configurable: true,
448
+ writable: true,
449
+ value: (...args) => new LPosCommand(args, this.opts).exec(this.client)
450
+ });
429
451
  /**
430
452
  * @see https://redis.io/commands/lpush
431
453
  */
@@ -1064,4 +1086,7 @@ export class Redis {
1064
1086
  this.client = client;
1065
1087
  this.opts = opts;
1066
1088
  }
1089
+ createScript(script) {
1090
+ return new Script(this, script);
1091
+ }
1067
1092
  }
@@ -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
- }
@@ -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
  /**
5
5
  * Serverless redis client for upstash.
6
6
  */
@@ -20,8 +20,9 @@ export class Redis extends core.Redis {
20
20
  /\r|\n/.test(configOrRequester.token)) {
21
21
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
22
22
  }
23
- const client = defaultRequester({
23
+ const client = new HttpClient({
24
24
  baseUrl: configOrRequester.url,
25
+ retry: configOrRequester.retry,
25
26
  headers: { authorization: `Bearer ${configOrRequester.token}` },
26
27
  // agent: configOrRequester.agent,
27
28
  });
@@ -39,42 +40,29 @@ export class Redis extends core.Redis {
39
40
  * your environment using `process.env`.
40
41
  */
41
42
  static fromEnv(config) {
43
+ let url = undefined;
44
+ let token = undefined;
42
45
  // @ts-ignore process will be defined in node
43
- if (typeof process?.env === "undefined") {
44
- throw new Error('Unable to get environment variables, `process.env` is undefined. If you are deploying to cloudflare, please import from "@upstash/redis/cloudflare" instead');
46
+ if (typeof process !== "undefined") {
47
+ // @ts-ignore process will be defined in node
48
+ url = process?.env["UPSTASH_REDIS_REST_URL"];
49
+ // @ts-ignore process will be defined in node
50
+ token = process?.env["UPSTASH_REDIS_REST_TOKEN"];
51
+ }
52
+ // fallback for Vite https://vitejs.dev/guide/env-and-mode.html
53
+ if (!url) {
54
+ url = import.meta.env["VITE_UPSTASH_REDIS_REST_URL"];
45
55
  }
46
- // @ts-ignore process will be defined in node
47
- const url = process?.env["UPSTASH_REDIS_REST_URL"];
48
56
  if (!url) {
49
57
  throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
50
58
  }
51
- // @ts-ignore process will be defined in node
52
- const token = process?.env["UPSTASH_REDIS_REST_TOKEN"];
59
+ // fallback for Vite https://vitejs.dev/guide/env-and-mode.html
60
+ if (!token) {
61
+ token = import.meta.env.VITE_UPSTASH_REDIS_REST_TOKEN;
62
+ }
53
63
  if (!token) {
54
64
  throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
55
65
  }
56
- return new Redis({ url, token, ...config });
66
+ return new Redis({ ...config, url, token });
57
67
  }
58
68
  }
59
- function defaultRequester(config) {
60
- return {
61
- request: async function (req) {
62
- if (!req.path) {
63
- req.path = [];
64
- }
65
- const res = await fetch([config.baseUrl, ...req.path].join("/"), {
66
- method: "POST",
67
- headers: { "Content-Type": "application/json", ...config.headers },
68
- body: JSON.stringify(req.body),
69
- keepalive: true,
70
- // @ts-ignore
71
- agent: config.agent,
72
- });
73
- const body = (await res.json());
74
- if (!res.ok) {
75
- throw new UpstashError(body.error);
76
- }
77
- return body;
78
- },
79
- };
80
- }
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.b4d4a1cc",
6
+ "version": "v0.0.0-ci.b984c0e9-20220826",
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": "5 KB"
44
- },
45
- {
46
- "path": "esm/platforms/fastly.js",
47
- "limit": "5 KB"
48
- },
49
- {
50
- "path": "esm/platforms/cloudflare.js",
51
- "limit": "5 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",