@upstash/redis 0.0.0-ci.b4d4a1cc → 0.0.0-ci.b8efaf97-20230119

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/README.md +43 -279
  2. package/esm/deps/deno.land/x/base64@v0.2.1/base.js +100 -0
  3. package/esm/deps/deno.land/x/base64@v0.2.1/base64url.js +9 -0
  4. package/esm/deps/deno.land/x/sha1@v1.0.3/deps.js +1 -0
  5. package/esm/deps/deno.land/x/sha1@v1.0.3/mod.js +191 -0
  6. package/esm/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +50 -0
  7. package/esm/pkg/commands/getdel.js +9 -0
  8. package/esm/pkg/commands/hgetall.js +0 -4
  9. package/esm/pkg/commands/hrandfield.js +39 -0
  10. package/esm/pkg/commands/lmove.js +9 -0
  11. package/esm/pkg/commands/lpos.js +19 -0
  12. package/esm/pkg/commands/mod.js +5 -0
  13. package/esm/pkg/commands/scan.js +3 -0
  14. package/esm/pkg/commands/sdiffstore.js +1 -1
  15. package/esm/pkg/commands/set.js +16 -4
  16. package/esm/pkg/commands/smismember.js +9 -0
  17. package/esm/pkg/commands/zdiffstore.js +9 -0
  18. package/esm/pkg/commands/zmscore.js +10 -0
  19. package/esm/pkg/commands/zrange.js +6 -0
  20. package/esm/pkg/http.js +133 -8
  21. package/esm/pkg/pipeline.js +80 -6
  22. package/esm/pkg/redis.js +125 -2
  23. package/esm/pkg/script.js +77 -0
  24. package/esm/platforms/cloudflare.js +14 -26
  25. package/esm/platforms/fastly.js +5 -25
  26. package/esm/platforms/node_with_fetch.js +31 -26
  27. package/esm/platforms/nodejs.js +26 -26
  28. package/esm/version.js +1 -0
  29. package/package.json +1 -39
  30. package/script/deps/deno.land/x/base64@v0.2.1/base.js +104 -0
  31. package/script/deps/deno.land/x/base64@v0.2.1/base64url.js +13 -0
  32. package/script/deps/deno.land/x/sha1@v1.0.3/deps.js +6 -0
  33. package/script/deps/deno.land/x/sha1@v1.0.3/mod.js +196 -0
  34. package/script/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +55 -0
  35. package/script/pkg/commands/getdel.js +13 -0
  36. package/script/pkg/commands/hgetall.js +0 -4
  37. package/script/pkg/commands/hrandfield.js +43 -0
  38. package/script/pkg/commands/lmove.js +13 -0
  39. package/script/pkg/commands/lpos.js +23 -0
  40. package/script/pkg/commands/mod.js +5 -0
  41. package/script/pkg/commands/scan.js +3 -0
  42. package/script/pkg/commands/sdiffstore.js +1 -1
  43. package/script/pkg/commands/set.js +16 -4
  44. package/script/pkg/commands/smismember.js +13 -0
  45. package/script/pkg/commands/zdiffstore.js +13 -0
  46. package/script/pkg/commands/zmscore.js +14 -0
  47. package/script/pkg/commands/zrange.js +6 -0
  48. package/script/pkg/http.js +133 -8
  49. package/script/pkg/pipeline.js +79 -5
  50. package/script/pkg/redis.js +124 -1
  51. package/script/pkg/script.js +81 -0
  52. package/script/platforms/cloudflare.js +14 -26
  53. package/script/platforms/fastly.js +5 -25
  54. package/script/platforms/node_with_fetch.js +31 -26
  55. package/script/platforms/nodejs.js +26 -26
  56. package/script/version.js +4 -0
  57. package/types/deps/deno.land/x/base64@v0.2.1/base.d.ts +5 -0
  58. package/types/deps/deno.land/x/base64@v0.2.1/base64url.d.ts +1 -0
  59. package/types/deps/deno.land/x/sha1@v1.0.3/deps.d.ts +1 -0
  60. package/types/deps/deno.land/x/sha1@v1.0.3/mod.d.ts +26 -0
  61. package/types/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.d.ts +3 -0
  62. package/types/pkg/commands/bitop.d.ts +0 -1
  63. package/types/pkg/commands/bitpos.d.ts +1 -1
  64. package/types/pkg/commands/getdel.d.ts +7 -0
  65. package/types/pkg/commands/hrandfield.d.ts +9 -0
  66. package/types/pkg/commands/lmove.d.ts +12 -0
  67. package/types/pkg/commands/lpop.d.ts +1 -1
  68. package/types/pkg/commands/lpos.d.ts +15 -0
  69. package/types/pkg/commands/mget.d.ts +1 -1
  70. package/types/pkg/commands/mod.d.ts +5 -0
  71. package/types/pkg/commands/rpop.d.ts +2 -2
  72. package/types/pkg/commands/scan.d.ts +1 -0
  73. package/types/pkg/commands/sdiffstore.d.ts +1 -1
  74. package/types/pkg/commands/set.d.ts +31 -2
  75. package/types/pkg/commands/smembers.d.ts +2 -2
  76. package/types/pkg/commands/smismember.d.ts +7 -0
  77. package/types/pkg/commands/spop.d.ts +2 -2
  78. package/types/pkg/commands/zadd.d.ts +1 -1
  79. package/types/pkg/commands/zdiffstore.d.ts +7 -0
  80. package/types/pkg/commands/zmscore.d.ts +7 -0
  81. package/types/pkg/commands/zrange.d.ts +7 -0
  82. package/types/pkg/http.d.ts +63 -5
  83. package/types/pkg/pipeline.d.ts +46 -9
  84. package/types/pkg/redis.d.ts +65 -9
  85. package/types/pkg/script.d.ts +42 -0
  86. package/types/pkg/types.d.ts +17 -0
  87. package/types/platforms/cloudflare.d.ts +8 -3
  88. package/types/platforms/fastly.d.ts +2 -2
  89. package/types/platforms/node_with_fetch.d.ts +2 -2
  90. package/types/platforms/nodejs.d.ts +18 -2
  91. package/types/version.d.ts +1 -0
package/esm/pkg/redis.js CHANGED
@@ -1,5 +1,8 @@
1
- import { AppendCommand, BitCountCommand, BitOpCommand, BitPosCommand, DBSizeCommand, DecrByCommand, DecrCommand, DelCommand, EchoCommand, EvalCommand, EvalshaCommand, ExistsCommand, ExpireAtCommand, ExpireCommand, FlushAllCommand, FlushDBCommand, GetBitCommand, GetCommand, GetRangeCommand, GetSetCommand, HDelCommand, HExistsCommand, HGetAllCommand, HGetCommand, HIncrByCommand, HIncrByFloatCommand, HKeysCommand, HLenCommand, HMGetCommand, HMSetCommand, HScanCommand, HSetCommand, HSetNXCommand, HStrLenCommand, HValsCommand, IncrByCommand, IncrByFloatCommand, IncrCommand, KeysCommand, LIndexCommand, LInsertCommand, LLenCommand, LPopCommand, LPushCommand, LPushXCommand, LRangeCommand, LRemCommand, LSetCommand, LTrimCommand, MGetCommand, MSetCommand, MSetNXCommand, PersistCommand, PExpireAtCommand, PExpireCommand, PingCommand, PSetEXCommand, PTtlCommand, PublishCommand, RandomKeyCommand, RenameCommand, RenameNXCommand, RPopCommand, RPushCommand, RPushXCommand, SAddCommand, ScanCommand, SCardCommand, ScriptExistsCommand, ScriptFlushCommand, ScriptLoadCommand, SDiffCommand, SDiffStoreCommand, SetBitCommand, SetCommand, SetExCommand, SetNxCommand, SetRangeCommand, SInterCommand, SInterStoreCommand, SIsMemberCommand, SMembersCommand, SMoveCommand, SPopCommand, SRandMemberCommand, SRemCommand, SScanCommand, StrLenCommand, SUnionCommand, SUnionStoreCommand, TimeCommand, TouchCommand, TtlCommand, TypeCommand, UnlinkCommand, ZAddCommand, ZCardCommand, ZCountCommand, ZIncrByCommand, ZInterStoreCommand, ZLexCountCommand, ZPopMaxCommand, ZPopMinCommand, ZRangeCommand, ZRankCommand, ZRemCommand, ZRemRangeByLexCommand, ZRemRangeByRankCommand, ZRemRangeByScoreCommand, ZRevRankCommand, ZScanCommand, ZScoreCommand, ZUnionStoreCommand, } from "./commands/mod.js";
1
+ import { AppendCommand, BitCountCommand, BitOpCommand, BitPosCommand, DBSizeCommand, DecrByCommand, DecrCommand, DelCommand, EchoCommand, EvalCommand, EvalshaCommand, ExistsCommand, ExpireAtCommand, ExpireCommand, FlushAllCommand, FlushDBCommand, GetBitCommand, GetCommand, GetDelCommand, GetRangeCommand, GetSetCommand, HDelCommand, HExistsCommand, HGetAllCommand, HGetCommand, HIncrByCommand, HIncrByFloatCommand, HKeysCommand, HLenCommand, HMGetCommand, HMSetCommand, HRandFieldCommand, HScanCommand, HSetCommand, HSetNXCommand, HStrLenCommand, HValsCommand, IncrByCommand, IncrByFloatCommand, IncrCommand, KeysCommand, LIndexCommand, LInsertCommand, LLenCommand, LMoveCommand, LPopCommand, LPosCommand, LPushCommand, LPushXCommand, LRangeCommand, LRemCommand, LSetCommand, LTrimCommand, MGetCommand, MSetCommand, MSetNXCommand, PersistCommand, PExpireAtCommand, PExpireCommand, PingCommand, PSetEXCommand, PTtlCommand, PublishCommand, RandomKeyCommand, RenameCommand, RenameNXCommand, RPopCommand, RPushCommand, RPushXCommand, SAddCommand, ScanCommand, SCardCommand, ScriptExistsCommand, ScriptFlushCommand, ScriptLoadCommand, SDiffCommand, SDiffStoreCommand, SetBitCommand, SetCommand, SetExCommand, SetNxCommand, SetRangeCommand, SInterCommand, SInterStoreCommand, SIsMemberCommand, SMembersCommand, SMIsMemberCommand, SMoveCommand, SPopCommand, SRandMemberCommand, SRemCommand, SScanCommand, StrLenCommand, SUnionCommand, SUnionStoreCommand, TimeCommand, TouchCommand, TtlCommand, TypeCommand, UnlinkCommand, ZAddCommand, ZCardCommand, ZCountCommand, ZIncrByCommand, ZInterStoreCommand, ZLexCountCommand, ZPopMaxCommand, ZPopMinCommand, ZRangeCommand, ZRankCommand, ZRemCommand, ZRemRangeByLexCommand, ZRemRangeByRankCommand, ZRemRangeByScoreCommand, ZRevRankCommand, ZScanCommand, ZScoreCommand, ZUnionStoreCommand, } from "./commands/mod.js";
2
2
  import { Pipeline } from "./pipeline.js";
3
+ import { Script } from "./script.js";
4
+ import { ZMScoreCommand } from "./commands/zmscore.js";
5
+ import { ZDiffStoreCommand } from "./commands/zdiffstore.js";
3
6
  /**
4
7
  * Serverless redis client for upstash.
5
8
  */
@@ -28,6 +31,36 @@ export class Redis {
28
31
  writable: true,
29
32
  value: void 0
30
33
  });
34
+ /**
35
+ * Wrap a new middleware around the HTTP client.
36
+ */
37
+ Object.defineProperty(this, "use", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: (middleware) => {
42
+ const makeRequest = this.client.request.bind(this.client);
43
+ this.client.request = (req) => middleware(req, makeRequest);
44
+ }
45
+ });
46
+ /**
47
+ * Technically this is not private, we can hide it from intellisense by doing this
48
+ */
49
+ Object.defineProperty(this, "addTelemetry", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: (telemetry) => {
54
+ try {
55
+ // @ts-ignore - The `Requester` interface does not know about this method but it will be there
56
+ // as long as the user uses the standard HttpClient
57
+ this.client.mergeTelemetry(telemetry);
58
+ }
59
+ catch {
60
+ // ignore
61
+ }
62
+ }
63
+ });
31
64
  /**
32
65
  * Create a new pipeline that allows you to send requests in bulk.
33
66
  *
@@ -37,7 +70,30 @@ export class Redis {
37
70
  enumerable: true,
38
71
  configurable: true,
39
72
  writable: true,
40
- value: () => new Pipeline(this.client, this.opts)
73
+ value: () => new Pipeline({
74
+ client: this.client,
75
+ commandOptions: this.opts,
76
+ multiExec: false,
77
+ })
78
+ });
79
+ /**
80
+ * Create a new transaction to allow executing multiple steps atomically.
81
+ *
82
+ * All the commands in a transaction are serialized and executed sequentially. A request sent by
83
+ * another client will never be served in the middle of the execution of a Redis Transaction. This
84
+ * guarantees that the commands are executed as a single isolated operation.
85
+ *
86
+ * @see {@link Pipeline}
87
+ */
88
+ Object.defineProperty(this, "multi", {
89
+ enumerable: true,
90
+ configurable: true,
91
+ writable: true,
92
+ value: () => new Pipeline({
93
+ client: this.client,
94
+ commandOptions: this.opts,
95
+ multiExec: true,
96
+ })
41
97
  });
42
98
  /**
43
99
  * @see https://redis.io/commands/append
@@ -201,6 +257,15 @@ export class Redis {
201
257
  writable: true,
202
258
  value: (...args) => new GetBitCommand(args, this.opts).exec(this.client)
203
259
  });
260
+ /**
261
+ * @see https://redis.io/commands/getdel
262
+ */
263
+ Object.defineProperty(this, "getdel", {
264
+ enumerable: true,
265
+ configurable: true,
266
+ writable: true,
267
+ value: (...args) => new GetDelCommand(args, this.opts).exec(this.client)
268
+ });
204
269
  /**
205
270
  * @see https://redis.io/commands/getrange
206
271
  */
@@ -309,6 +374,16 @@ export class Redis {
309
374
  writable: true,
310
375
  value: (key, kv) => new HMSetCommand([key, kv], this.opts).exec(this.client)
311
376
  });
377
+ /**
378
+ * @see https://redis.io/commands/hrandfield
379
+ */
380
+ Object.defineProperty(this, "hrandfield", {
381
+ enumerable: true,
382
+ configurable: true,
383
+ writable: true,
384
+ value: (key, count, withValues) => new HRandFieldCommand([key, count, withValues], this.opts)
385
+ .exec(this.client)
386
+ });
312
387
  /**
313
388
  * @see https://redis.io/commands/hscan
314
389
  */
@@ -417,6 +492,15 @@ export class Redis {
417
492
  writable: true,
418
493
  value: (...args) => new LLenCommand(args, this.opts).exec(this.client)
419
494
  });
495
+ /**
496
+ * @see https://redis.io/commands/lmove
497
+ */
498
+ Object.defineProperty(this, "lmove", {
499
+ enumerable: true,
500
+ configurable: true,
501
+ writable: true,
502
+ value: (...args) => new LMoveCommand(args, this.opts).exec(this.client)
503
+ });
420
504
  /**
421
505
  * @see https://redis.io/commands/lpop
422
506
  */
@@ -426,6 +510,15 @@ export class Redis {
426
510
  writable: true,
427
511
  value: (...args) => new LPopCommand(args, this.opts).exec(this.client)
428
512
  });
513
+ /**
514
+ * @see https://redis.io/commands/lpos
515
+ */
516
+ Object.defineProperty(this, "lpos", {
517
+ enumerable: true,
518
+ configurable: true,
519
+ writable: true,
520
+ value: (...args) => new LPosCommand(args, this.opts).exec(this.client)
521
+ });
429
522
  /**
430
523
  * @see https://redis.io/commands/lpush
431
524
  */
@@ -768,6 +861,15 @@ export class Redis {
768
861
  writable: true,
769
862
  value: (key, member) => new SIsMemberCommand([key, member], this.opts).exec(this.client)
770
863
  });
864
+ /**
865
+ * @see https://redis.io/commands/smismember
866
+ */
867
+ Object.defineProperty(this, "smismember", {
868
+ enumerable: true,
869
+ configurable: true,
870
+ writable: true,
871
+ value: (key, members) => new SMIsMemberCommand([key, members], this.opts).exec(this.client)
872
+ });
771
873
  /**
772
874
  * @see https://redis.io/commands/smembers
773
875
  */
@@ -926,6 +1028,15 @@ export class Redis {
926
1028
  writable: true,
927
1029
  value: (...args) => new ZCountCommand(args, this.opts).exec(this.client)
928
1030
  });
1031
+ /**
1032
+ * @see https://redis.io/commands/zdiffstore
1033
+ */
1034
+ Object.defineProperty(this, "zdiffstore", {
1035
+ enumerable: true,
1036
+ configurable: true,
1037
+ writable: true,
1038
+ value: (...args) => new ZDiffStoreCommand(args, this.opts).exec(this.client)
1039
+ });
929
1040
  /**
930
1041
  * @see https://redis.io/commands/zincrby
931
1042
  */
@@ -953,6 +1064,15 @@ export class Redis {
953
1064
  writable: true,
954
1065
  value: (...args) => new ZLexCountCommand(args, this.opts).exec(this.client)
955
1066
  });
1067
+ /**
1068
+ * @see https://redis.io/commands/zmscore
1069
+ */
1070
+ Object.defineProperty(this, "zmscore", {
1071
+ enumerable: true,
1072
+ configurable: true,
1073
+ writable: true,
1074
+ value: (...args) => new ZMScoreCommand(args, this.opts).exec(this.client)
1075
+ });
956
1076
  /**
957
1077
  * @see https://redis.io/commands/zpopmax
958
1078
  */
@@ -1064,4 +1184,7 @@ export class Redis {
1064
1184
  this.client = client;
1065
1185
  this.opts = opts;
1066
1186
  }
1187
+ createScript(script) {
1188
+ return new Script(this, script);
1189
+ }
1067
1190
  }
@@ -0,0 +1,77 @@
1
+ import { sha1 as digest } from "../deps/deno.land/x/sha1@v1.0.3/mod.js";
2
+ /**
3
+ * Creates a new script.
4
+ *
5
+ * Scripts offer the ability to optimistically try to execute a script without having to send the
6
+ * entire script to the server. If the script is loaded on the server, it tries again by sending
7
+ * the entire script. Afterwards, the script is cached on the server.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const redis = new Redis({...})
12
+ *
13
+ * const script = redis.createScript<string>("return ARGV[1];")
14
+ * const arg1 = await script.eval([], ["Hello World"])
15
+ * assertEquals(arg1, "Hello World")
16
+ * ```
17
+ */
18
+ export class Script {
19
+ constructor(redis, script) {
20
+ Object.defineProperty(this, "script", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "sha1", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "redis", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ this.redis = redis;
39
+ this.sha1 = this.digest(script);
40
+ this.script = script;
41
+ }
42
+ /**
43
+ * Send an `EVAL` command to redis.
44
+ */
45
+ async eval(keys, args) {
46
+ return await this.redis.eval(this.script, keys, args);
47
+ }
48
+ /**
49
+ * Calculates the sha1 hash of the script and then calls `EVALSHA`.
50
+ */
51
+ async evalsha(keys, args) {
52
+ return await this.redis.evalsha(this.sha1, keys, args);
53
+ }
54
+ /**
55
+ * Optimistically try to run `EVALSHA` first.
56
+ * If the script is not loaded in redis, it will fall back and try again with `EVAL`.
57
+ *
58
+ * Following calls will be able to use the cached script
59
+ */
60
+ async exec(keys, args) {
61
+ const res = await this.redis.evalsha(this.sha1, keys, args).catch(async (err) => {
62
+ if (err instanceof Error &&
63
+ err.message.toLowerCase().includes("noscript")) {
64
+ return await this.redis.eval(this.script, keys, args);
65
+ }
66
+ throw err;
67
+ });
68
+ return res;
69
+ }
70
+ /**
71
+ * Compute the sha1 hash of the script and return its hex representation.
72
+ */
73
+ digest(s) {
74
+ const hash = digest(s, "utf8", "hex");
75
+ return typeof hash === "string" ? hash : new TextDecoder().decode(hash);
76
+ }
77
+ }
@@ -1,5 +1,6 @@
1
1
  import * as core from "../pkg/redis.js";
2
- import { UpstashError } from "../pkg/error.js";
2
+ import { HttpClient } from "../pkg/http.js";
3
+ import { VERSION } from "../version.js";
3
4
  /**
4
5
  * Serverless redis client for upstash.
5
6
  */
@@ -15,7 +16,7 @@ export class Redis extends core.Redis {
15
16
  * });
16
17
  * ```
17
18
  */
18
- constructor(config) {
19
+ constructor(config, env) {
19
20
  if (config.url.startsWith(" ") ||
20
21
  config.url.endsWith(" ") ||
21
22
  /\r|\n/.test(config.url)) {
@@ -26,9 +27,17 @@ export class Redis extends core.Redis {
26
27
  /\r|\n/.test(config.token)) {
27
28
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
28
29
  }
29
- const client = defaultRequester({
30
+ const telemetry = {};
31
+ if (!env?.UPSTASH_DISABLE_TELEMETRY) {
32
+ telemetry.platform = "cloudflare";
33
+ telemetry.sdk = `@upstash/redis@${VERSION}`;
34
+ }
35
+ const client = new HttpClient({
36
+ retry: config.retry,
30
37
  baseUrl: config.url,
31
38
  headers: { authorization: `Bearer ${config.token}` },
39
+ responseEncoding: config.responseEncoding,
40
+ telemetry,
32
41
  });
33
42
  super(client, {
34
43
  automaticDeserialization: config.automaticDeserialization,
@@ -44,9 +53,8 @@ export class Redis extends core.Redis {
44
53
  * ```ts
45
54
  * const redis = Redis.fromEnv(env)
46
55
  * ```
47
- *
48
56
  */
49
- static fromEnv(env) {
57
+ static fromEnv(env, opts) {
50
58
  // @ts-ignore These will be defined by cloudflare
51
59
  const url = env?.UPSTASH_REDIS_REST_URL ?? UPSTASH_REDIS_REST_URL;
52
60
  // @ts-ignore These will be defined by cloudflare
@@ -57,26 +65,6 @@ export class Redis extends core.Redis {
57
65
  if (!token) {
58
66
  throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`");
59
67
  }
60
- return new Redis({ url, token });
68
+ return new Redis({ ...opts, url, token }, env);
61
69
  }
62
70
  }
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,15 @@ 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 },
35
+ responseEncoding: config.responseEncoding,
34
36
  });
35
37
  super(client, {
36
38
  automaticDeserialization: config.automaticDeserialization,
37
39
  });
38
40
  }
39
41
  }
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,7 +1,21 @@
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
+ import { VERSION } from "../version.js";
4
5
  import "isomorphic-fetch";
6
+ // @ts-ignore Deno can't compile
7
+ // import https from "https";
8
+ // @ts-ignore Deno can't compile
9
+ // import http from "http";
10
+ // import "isomorphic-fetch";
11
+ /**
12
+ * Workaround for nodejs 14, where atob is not included in the standardlib
13
+ */
14
+ if (typeof atob === "undefined") {
15
+ global.atob = function (b64) {
16
+ return Buffer.from(b64, "base64").toString("utf-8");
17
+ };
18
+ }
5
19
  /**
6
20
  * Serverless redis client for upstash.
7
21
  */
@@ -21,10 +35,23 @@ export class Redis extends core.Redis {
21
35
  /\r|\n/.test(configOrRequester.token)) {
22
36
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
23
37
  }
24
- const client = defaultRequester({
38
+ const telemetry = {};
39
+ if (!process.env.UPSTASH_DISABLE_TELEMETRY) {
40
+ telemetry.runtime = `node@${process.version}`;
41
+ telemetry.platform = process.env.VERCEL
42
+ ? "vercel"
43
+ : process.env.AWS_REGION
44
+ ? "aws"
45
+ : "unknown";
46
+ telemetry.sdk = `@upstash/redis@${VERSION}`;
47
+ }
48
+ const client = new HttpClient({
25
49
  baseUrl: configOrRequester.url,
50
+ retry: configOrRequester.retry,
26
51
  headers: { authorization: `Bearer ${configOrRequester.token}` },
27
- // agent: configOrRequester.agent,
52
+ // agent: configOrRequester.agent,
53
+ responseEncoding: configOrRequester.responseEncoding,
54
+ telemetry,
28
55
  });
29
56
  super(client, {
30
57
  automaticDeserialization: configOrRequester.automaticDeserialization,
@@ -54,28 +81,6 @@ export class Redis extends core.Redis {
54
81
  if (!token) {
55
82
  throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
56
83
  }
57
- return new Redis({ url, token, ...config });
84
+ return new Redis({ ...config, url, token });
58
85
  }
59
86
  }
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,15 @@
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
+ import { VERSION } from "../version.js";
5
+ /**
6
+ * Workaround for nodejs 14, where atob is not included in the standardlib
7
+ */
8
+ if (typeof atob === "undefined") {
9
+ global.atob = function (b64) {
10
+ return Buffer.from(b64, "base64").toString("utf-8");
11
+ };
12
+ }
4
13
  /**
5
14
  * Serverless redis client for upstash.
6
15
  */
@@ -20,10 +29,23 @@ export class Redis extends core.Redis {
20
29
  /\r|\n/.test(configOrRequester.token)) {
21
30
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
22
31
  }
23
- const client = defaultRequester({
32
+ const telemetry = {};
33
+ if (!process.env.UPSTASH_DISABLE_TELEMETRY) {
34
+ telemetry.runtime = `node@${process.version}`;
35
+ telemetry.platform = process.env.VERCEL
36
+ ? "vercel"
37
+ : process.env.AWS_REGION
38
+ ? "aws"
39
+ : "unknown";
40
+ telemetry.sdk = `@upstash/redis@${VERSION}`;
41
+ }
42
+ const client = new HttpClient({
24
43
  baseUrl: configOrRequester.url,
44
+ retry: configOrRequester.retry,
25
45
  headers: { authorization: `Bearer ${configOrRequester.token}` },
26
- // agent: configOrRequester.agent,
46
+ agent: configOrRequester.agent,
47
+ responseEncoding: configOrRequester.responseEncoding,
48
+ telemetry,
27
49
  });
28
50
  super(client, {
29
51
  automaticDeserialization: configOrRequester.automaticDeserialization,
@@ -53,28 +75,6 @@ export class Redis extends core.Redis {
53
75
  if (!token) {
54
76
  throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`");
55
77
  }
56
- return new Redis({ url, token, ...config });
78
+ return new Redis({ ...config, url, token });
57
79
  }
58
80
  }
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/esm/version.js ADDED
@@ -0,0 +1 @@
1
+ export const VERSION = "v0.0.0-ci.b8efaf97-20230119";
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.b8efaf97-20230119",
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",