@upstash/redis 0.0.0-ci.5f29e95e → 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 (64) hide show
  1. package/README.md +14 -19
  2. package/esm/deps/deno.land/x/base64@v0.2.1/base.js +100 -0
  3. package/esm/deps/deno.land/x/base64@v0.2.1/base64url.js +9 -0
  4. package/esm/deps/deno.land/x/sha1@v1.0.3/deps.js +1 -0
  5. package/esm/deps/deno.land/x/sha1@v1.0.3/mod.js +191 -0
  6. package/esm/deps/denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js +50 -0
  7. package/esm/pkg/commands/getdel.js +9 -0
  8. package/esm/pkg/commands/hgetall.js +0 -4
  9. package/esm/pkg/commands/hrandfield.js +39 -0
  10. package/esm/pkg/commands/mod.js +2 -0
  11. package/esm/pkg/commands/scan.js +3 -0
  12. package/esm/pkg/commands/set.js +16 -4
  13. package/esm/pkg/commands/zmscore.js +10 -0
  14. package/esm/pkg/http.js +82 -2
  15. package/esm/pkg/pipeline.js +43 -6
  16. package/esm/pkg/redis.js +58 -2
  17. package/esm/pkg/script.js +77 -0
  18. package/esm/platforms/cloudflare.js +10 -2
  19. package/esm/platforms/fastly.js +1 -0
  20. package/esm/platforms/node_with_fetch.js +14 -1
  21. package/esm/platforms/nodejs.js +14 -1
  22. package/esm/version.js +1 -0
  23. package/package.json +1 -39
  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/getdel.js +13 -0
  30. package/script/pkg/commands/hgetall.js +0 -4
  31. package/script/pkg/commands/hrandfield.js +43 -0
  32. package/script/pkg/commands/mod.js +2 -0
  33. package/script/pkg/commands/scan.js +3 -0
  34. package/script/pkg/commands/set.js +16 -4
  35. package/script/pkg/commands/zmscore.js +14 -0
  36. package/script/pkg/http.js +82 -2
  37. package/script/pkg/pipeline.js +42 -5
  38. package/script/pkg/redis.js +57 -1
  39. package/script/pkg/script.js +81 -0
  40. package/script/platforms/cloudflare.js +10 -2
  41. package/script/platforms/fastly.js +1 -0
  42. package/script/platforms/node_with_fetch.js +14 -1
  43. package/script/platforms/nodejs.js +14 -1
  44. package/script/version.js +4 -0
  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/getdel.d.ts +7 -0
  51. package/types/pkg/commands/hrandfield.d.ts +9 -0
  52. package/types/pkg/commands/mod.d.ts +2 -0
  53. package/types/pkg/commands/scan.d.ts +1 -0
  54. package/types/pkg/commands/set.d.ts +31 -2
  55. package/types/pkg/commands/zmscore.d.ts +7 -0
  56. package/types/pkg/http.d.ts +52 -4
  57. package/types/pkg/pipeline.d.ts +20 -3
  58. package/types/pkg/redis.d.ts +29 -1
  59. package/types/pkg/script.d.ts +42 -0
  60. package/types/platforms/cloudflare.d.ts +8 -7
  61. package/types/platforms/fastly.d.ts +2 -6
  62. package/types/platforms/node_with_fetch.d.ts +2 -21
  63. package/types/platforms/nodejs.d.ts +3 -6
  64. package/types/version.d.ts +1 -0
@@ -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, 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";
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
  */
@@ -1011,6 +1038,15 @@ export class Pipeline {
1011
1038
  writable: true,
1012
1039
  value: (...args) => this.chain(new ZLexCountCommand(args, this.commandOptions))
1013
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
+ });
1014
1050
  /**
1015
1051
  * @see https://redis.io/commands/zpopmax
1016
1052
  */
@@ -1119,9 +1155,10 @@ export class Pipeline {
1119
1155
  writable: true,
1120
1156
  value: (...args) => this.chain(new ZUnionStoreCommand(args, this.commandOptions))
1121
1157
  });
1122
- this.client = client;
1158
+ this.client = opts.client;
1123
1159
  this.commands = [];
1124
- this.commandOptions = commandOptions;
1160
+ this.commandOptions = opts.commandOptions;
1161
+ this.multiExec = opts.multiExec ?? false;
1125
1162
  }
1126
1163
  /**
1127
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, 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";
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
  */
@@ -49,7 +51,30 @@ export class Redis {
49
51
  enumerable: true,
50
52
  configurable: true,
51
53
  writable: true,
52
- 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
+ })
53
78
  });
54
79
  /**
55
80
  * @see https://redis.io/commands/append
@@ -213,6 +238,15 @@ export class Redis {
213
238
  writable: true,
214
239
  value: (...args) => new GetBitCommand(args, this.opts).exec(this.client)
215
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
+ });
216
250
  /**
217
251
  * @see https://redis.io/commands/getrange
218
252
  */
@@ -321,6 +355,16 @@ export class Redis {
321
355
  writable: true,
322
356
  value: (key, kv) => new HMSetCommand([key, kv], this.opts).exec(this.client)
323
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
+ });
324
368
  /**
325
369
  * @see https://redis.io/commands/hscan
326
370
  */
@@ -974,6 +1018,15 @@ export class Redis {
974
1018
  writable: true,
975
1019
  value: (...args) => new ZLexCountCommand(args, this.opts).exec(this.client)
976
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
+ });
977
1030
  /**
978
1031
  * @see https://redis.io/commands/zpopmax
979
1032
  */
@@ -1085,4 +1138,7 @@ export class Redis {
1085
1138
  this.client = client;
1086
1139
  this.opts = opts;
1087
1140
  }
1141
+ createScript(script) {
1142
+ return new Script(this, script);
1143
+ }
1088
1144
  }
@@ -0,0 +1,77 @@
1
+ import { sha1 as digest } from "../deps/deno.land/x/sha1@v1.0.3/mod.js";
2
+ /**
3
+ * Creates a new script.
4
+ *
5
+ * Scripts offer the ability to optimistically try to execute a script without having to send the
6
+ * entire script to the server. If the script is loaded on the server, it tries again by sending
7
+ * the entire script. Afterwards, the script is cached on the server.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const redis = new Redis({...})
12
+ *
13
+ * const script = redis.createScript<string>("return ARGV[1];")
14
+ * const arg1 = await script.eval([], ["Hello World"])
15
+ * assertEquals(arg1, "Hello World")
16
+ * ```
17
+ */
18
+ export class Script {
19
+ constructor(redis, script) {
20
+ Object.defineProperty(this, "script", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "sha1", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "redis", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ this.redis = redis;
39
+ this.sha1 = this.digest(script);
40
+ this.script = script;
41
+ }
42
+ /**
43
+ * Send an `EVAL` command to redis.
44
+ */
45
+ async eval(keys, args) {
46
+ return await this.redis.eval(this.script, keys, args);
47
+ }
48
+ /**
49
+ * Calculates the sha1 hash of the script and then calls `EVALSHA`.
50
+ */
51
+ async evalsha(keys, args) {
52
+ return await this.redis.evalsha(this.sha1, keys, args);
53
+ }
54
+ /**
55
+ * Optimistically try to run `EVALSHA` first.
56
+ * If the script is not loaded in redis, it will fall back and try again with `EVAL`.
57
+ *
58
+ * Following calls will be able to use the cached script
59
+ */
60
+ async exec(keys, args) {
61
+ const res = await this.redis.evalsha(this.sha1, keys, args).catch(async (err) => {
62
+ if (err instanceof Error &&
63
+ err.message.toLowerCase().includes("noscript")) {
64
+ return await this.redis.eval(this.script, keys, args);
65
+ }
66
+ throw err;
67
+ });
68
+ return res;
69
+ }
70
+ /**
71
+ * Compute the sha1 hash of the script and return its hex representation.
72
+ */
73
+ digest(s) {
74
+ const hash = digest(s, "utf8", "hex");
75
+ return typeof hash === "string" ? hash : new TextDecoder().decode(hash);
76
+ }
77
+ }
@@ -1,5 +1,6 @@
1
1
  import * as core from "../pkg/redis.js";
2
2
  import { HttpClient } from "../pkg/http.js";
3
+ import { VERSION } from "../version.js";
3
4
  /**
4
5
  * Serverless redis client for upstash.
5
6
  */
@@ -15,7 +16,7 @@ export class Redis extends core.Redis {
15
16
  * });
16
17
  * ```
17
18
  */
18
- constructor(config) {
19
+ constructor(config, env) {
19
20
  if (config.url.startsWith(" ") ||
20
21
  config.url.endsWith(" ") ||
21
22
  /\r|\n/.test(config.url)) {
@@ -26,10 +27,17 @@ export class Redis extends core.Redis {
26
27
  /\r|\n/.test(config.token)) {
27
28
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
28
29
  }
30
+ const telemetry = {};
31
+ if (!env?.UPSTASH_DISABLE_TELEMETRY) {
32
+ telemetry.platform = "cloudflare";
33
+ telemetry.sdk = `@upstash/redis@${VERSION}`;
34
+ }
29
35
  const client = new HttpClient({
30
36
  retry: config.retry,
31
37
  baseUrl: config.url,
32
38
  headers: { authorization: `Bearer ${config.token}` },
39
+ responseEncoding: config.responseEncoding,
40
+ telemetry,
33
41
  });
34
42
  super(client, {
35
43
  automaticDeserialization: config.automaticDeserialization,
@@ -57,6 +65,6 @@ export class Redis extends core.Redis {
57
65
  if (!token) {
58
66
  throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`");
59
67
  }
60
- return new Redis({ ...opts, url, token });
68
+ return new Redis({ ...opts, url, token }, env);
61
69
  }
62
70
  }
@@ -32,6 +32,7 @@ export class Redis extends core.Redis {
32
32
  retry: config.retry,
33
33
  headers: { authorization: `Bearer ${config.token}` },
34
34
  options: { backend: config.backend },
35
+ responseEncoding: config.responseEncoding,
35
36
  });
36
37
  super(client, {
37
38
  automaticDeserialization: config.automaticDeserialization,
@@ -1,6 +1,7 @@
1
1
  // deno-lint-ignore-file
2
2
  import * as core from "../pkg/redis.js";
3
3
  import { HttpClient, } from "../pkg/http.js";
4
+ import { VERSION } from "../version.js";
4
5
  import "isomorphic-fetch";
5
6
  /**
6
7
  * Serverless redis client for upstash.
@@ -21,11 +22,23 @@ export class Redis extends core.Redis {
21
22
  /\r|\n/.test(configOrRequester.token)) {
22
23
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
23
24
  }
25
+ const telemetry = {};
26
+ if (!process.env.UPSTASH_DISABLE_TELEMETRY) {
27
+ telemetry.runtime = `node@${process.version}`;
28
+ telemetry.platform = process.env.VERCEL
29
+ ? "vercel"
30
+ : process.env.AWS_REGION
31
+ ? "aws"
32
+ : "unknown";
33
+ telemetry.sdk = `@upstash/redis@${VERSION}`;
34
+ }
24
35
  const client = new HttpClient({
25
36
  baseUrl: configOrRequester.url,
26
37
  retry: configOrRequester.retry,
27
38
  headers: { authorization: `Bearer ${configOrRequester.token}` },
28
- // agent: configOrRequester.agent,
39
+ // agent: configOrRequester.agent,
40
+ responseEncoding: configOrRequester.responseEncoding,
41
+ telemetry,
29
42
  });
30
43
  super(client, {
31
44
  automaticDeserialization: configOrRequester.automaticDeserialization,
@@ -1,6 +1,7 @@
1
1
  // deno-lint-ignore-file
2
2
  import * as core from "../pkg/redis.js";
3
3
  import { HttpClient, } from "../pkg/http.js";
4
+ import { VERSION } from "../version.js";
4
5
  /**
5
6
  * Serverless redis client for upstash.
6
7
  */
@@ -20,11 +21,23 @@ export class Redis extends core.Redis {
20
21
  /\r|\n/.test(configOrRequester.token)) {
21
22
  console.warn("The redis token contains whitespace or newline, which can cause errors!");
22
23
  }
24
+ const telemetry = {};
25
+ if (!process.env.UPSTASH_DISABLE_TELEMETRY) {
26
+ telemetry.runtime = `node@${process.version}`;
27
+ telemetry.platform = process.env.VERCEL
28
+ ? "vercel"
29
+ : process.env.AWS_REGION
30
+ ? "aws"
31
+ : "unknown";
32
+ telemetry.sdk = `@upstash/redis@${VERSION}`;
33
+ }
23
34
  const client = new HttpClient({
24
35
  baseUrl: configOrRequester.url,
25
36
  retry: configOrRequester.retry,
26
37
  headers: { authorization: `Bearer ${configOrRequester.token}` },
27
- // agent: configOrRequester.agent,
38
+ agent: configOrRequester.agent,
39
+ responseEncoding: configOrRequester.responseEncoding,
40
+ telemetry,
28
41
  });
29
42
  super(client, {
30
43
  automaticDeserialization: configOrRequester.automaticDeserialization,
package/esm/version.js ADDED
@@ -0,0 +1 @@
1
+ export const VERSION = "v0.0.0-ci.608697de-20221127";
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.5f29e95e",
6
+ "version": "v0.0.0-ci.608697de-20221127",
7
7
  "description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
8
8
  "repository": {
9
9
  "type": "git",
@@ -22,10 +22,6 @@
22
22
  "url": "https://github.com/upstash/upstash-redis/issues"
23
23
  },
24
24
  "homepage": "https://github.com/upstash/upstash-redis#readme",
25
- "devDependencies": {
26
- "@size-limit/preset-small-lib": "latest",
27
- "size-limit": "latest"
28
- },
29
25
  "dependencies": {
30
26
  "isomorphic-fetch": "^3.0.0"
31
27
  },
@@ -37,40 +33,6 @@
37
33
  "with-fetch": "./types/platforms/node_with_fetch.d.ts"
38
34
  }
39
35
  },
40
- "size-limit": [
41
- {
42
- "path": "esm/platforms/nodejs.js",
43
- "limit": "6 KB"
44
- },
45
- {
46
- "path": "esm/platforms/fastly.js",
47
- "limit": "6 KB"
48
- },
49
- {
50
- "path": "esm/platforms/cloudflare.js",
51
- "limit": "6 KB"
52
- },
53
- {
54
- "path": "esm/platforms/node_with_fetch.js",
55
- "limit": "15 KB"
56
- },
57
- {
58
- "path": "script/platforms/nodejs.js",
59
- "limit": "10 KB"
60
- },
61
- {
62
- "path": "script/platforms/fastly.js",
63
- "limit": "10 KB"
64
- },
65
- {
66
- "path": "script/platforms/cloudflare.js",
67
- "limit": "10 KB"
68
- },
69
- {
70
- "path": "script/platforms/node_with_fetch.js",
71
- "limit": "15 KB"
72
- }
73
- ],
74
36
  "exports": {
75
37
  ".": {
76
38
  "import": "./esm/platforms/nodejs.js",
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.init = void 0;
4
+ function getLengths(b64) {
5
+ const len = b64.length;
6
+ // if (len % 4 > 0) {
7
+ // throw new TypeError("Invalid string. Length must be a multiple of 4");
8
+ // }
9
+ // Trim off extra bytes after placeholder bytes are found
10
+ // See: https://github.com/beatgammit/base64-js/issues/42
11
+ let validLen = b64.indexOf("=");
12
+ if (validLen === -1) {
13
+ validLen = len;
14
+ }
15
+ const placeHoldersLen = validLen === len ? 0 : 4 - (validLen % 4);
16
+ return [validLen, placeHoldersLen];
17
+ }
18
+ function init(lookup, revLookup, urlsafe = false) {
19
+ function _byteLength(validLen, placeHoldersLen) {
20
+ return Math.floor(((validLen + placeHoldersLen) * 3) / 4 - placeHoldersLen);
21
+ }
22
+ function tripletToBase64(num) {
23
+ return (lookup[(num >> 18) & 0x3f] +
24
+ lookup[(num >> 12) & 0x3f] +
25
+ lookup[(num >> 6) & 0x3f] +
26
+ lookup[num & 0x3f]);
27
+ }
28
+ function encodeChunk(buf, start, end) {
29
+ const out = new Array((end - start) / 3);
30
+ for (let i = start, curTriplet = 0; i < end; i += 3) {
31
+ out[curTriplet++] = tripletToBase64((buf[i] << 16) + (buf[i + 1] << 8) + buf[i + 2]);
32
+ }
33
+ return out.join("");
34
+ }
35
+ return {
36
+ // base64 is 4/3 + up to two characters of the original data
37
+ byteLength(b64) {
38
+ return _byteLength.apply(null, getLengths(b64));
39
+ },
40
+ toUint8Array(b64) {
41
+ const [validLen, placeHoldersLen] = getLengths(b64);
42
+ const buf = new Uint8Array(_byteLength(validLen, placeHoldersLen));
43
+ // If there are placeholders, only get up to the last complete 4 chars
44
+ const len = placeHoldersLen ? validLen - 4 : validLen;
45
+ let tmp;
46
+ let curByte = 0;
47
+ let i;
48
+ for (i = 0; i < len; i += 4) {
49
+ tmp = (revLookup[b64.charCodeAt(i)] << 18) |
50
+ (revLookup[b64.charCodeAt(i + 1)] << 12) |
51
+ (revLookup[b64.charCodeAt(i + 2)] << 6) |
52
+ revLookup[b64.charCodeAt(i + 3)];
53
+ buf[curByte++] = (tmp >> 16) & 0xff;
54
+ buf[curByte++] = (tmp >> 8) & 0xff;
55
+ buf[curByte++] = tmp & 0xff;
56
+ }
57
+ if (placeHoldersLen === 2) {
58
+ tmp = (revLookup[b64.charCodeAt(i)] << 2) |
59
+ (revLookup[b64.charCodeAt(i + 1)] >> 4);
60
+ buf[curByte++] = tmp & 0xff;
61
+ }
62
+ else if (placeHoldersLen === 1) {
63
+ tmp = (revLookup[b64.charCodeAt(i)] << 10) |
64
+ (revLookup[b64.charCodeAt(i + 1)] << 4) |
65
+ (revLookup[b64.charCodeAt(i + 2)] >> 2);
66
+ buf[curByte++] = (tmp >> 8) & 0xff;
67
+ buf[curByte++] = tmp & 0xff;
68
+ }
69
+ return buf;
70
+ },
71
+ fromUint8Array(buf) {
72
+ const maxChunkLength = 16383; // Must be multiple of 3
73
+ const len = buf.length;
74
+ const extraBytes = len % 3; // If we have 1 byte left, pad 2 bytes
75
+ const len2 = len - extraBytes;
76
+ const parts = new Array(Math.ceil(len2 / maxChunkLength) + (extraBytes ? 1 : 0));
77
+ let curChunk = 0;
78
+ let chunkEnd;
79
+ // Go through the array every three bytes, we'll deal with trailing stuff later
80
+ for (let i = 0; i < len2; i += maxChunkLength) {
81
+ chunkEnd = i + maxChunkLength;
82
+ parts[curChunk++] = encodeChunk(buf, i, chunkEnd > len2 ? len2 : chunkEnd);
83
+ }
84
+ let tmp;
85
+ // Pad the end with zeros, but make sure to not forget the extra bytes
86
+ if (extraBytes === 1) {
87
+ tmp = buf[len2];
88
+ parts[curChunk] = lookup[tmp >> 2] + lookup[(tmp << 4) & 0x3f];
89
+ if (!urlsafe)
90
+ parts[curChunk] += "==";
91
+ }
92
+ else if (extraBytes === 2) {
93
+ tmp = (buf[len2] << 8) | (buf[len2 + 1] & 0xff);
94
+ parts[curChunk] = lookup[tmp >> 10] +
95
+ lookup[(tmp >> 4) & 0x3f] +
96
+ lookup[(tmp << 2) & 0x3f];
97
+ if (!urlsafe)
98
+ parts[curChunk] += "=";
99
+ }
100
+ return parts.join("");
101
+ },
102
+ };
103
+ }
104
+ exports.init = init;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.fromUint8Array = exports.toUint8Array = exports.byteLength = void 0;
5
+ const base_js_1 = require("./base.js");
6
+ const lookup = [];
7
+ const revLookup = [];
8
+ const code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
9
+ for (let i = 0, l = code.length; i < l; ++i) {
10
+ lookup[i] = code[i];
11
+ revLookup[code.charCodeAt(i)] = i;
12
+ }
13
+ _a = (0, base_js_1.init)(lookup, revLookup, true), exports.byteLength = _a.byteLength, exports.toUint8Array = _a.toUint8Array, exports.fromUint8Array = _a.fromUint8Array;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decode = exports.encode = void 0;
4
+ var mod_js_1 = require("../../../denopkg.com/chiefbiiko/std-encoding@v1.0.0/mod.js");
5
+ Object.defineProperty(exports, "encode", { enumerable: true, get: function () { return mod_js_1.encode; } });
6
+ Object.defineProperty(exports, "decode", { enumerable: true, get: function () { return mod_js_1.decode; } });