@upstash/redis 1.22.1 → 1.23.1

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.
@@ -138,3 +138,5 @@ export * from "./zrevrank.js";
138
138
  export * from "./zscan.js";
139
139
  export * from "./zscore.js";
140
140
  export * from "./zunionstore.js";
141
+ export * from "./xadd.js";
142
+ export * from "./xrange.js";
@@ -0,0 +1,26 @@
1
+ import { Command } from "./command.js";
2
+ /**
3
+ * @see https://redis.io/commands/xadd
4
+ */
5
+ export class XAddCommand extends Command {
6
+ constructor([key, id, entries, opts], commandOptions) {
7
+ const command = ["XADD", key];
8
+ if (opts) {
9
+ if (opts.nomkStream) {
10
+ command.push("NOMKSTREAM");
11
+ }
12
+ if (opts.trim) {
13
+ command.push(opts.trim.type, opts.trim.comparison, opts.trim.threshold);
14
+ if (typeof opts.trim.limit !== "undefined") {
15
+ command.push("LIMIT", opts.trim.limit);
16
+ }
17
+ }
18
+ }
19
+ command.push(id);
20
+ // entries
21
+ Object.entries(entries).forEach(([k, v]) => {
22
+ command.push(k, v);
23
+ });
24
+ super(command, commandOptions);
25
+ }
26
+ }
@@ -0,0 +1,37 @@
1
+ import { Command } from "./command.js";
2
+ function deserialize(result) {
3
+ if (result.length === 0) {
4
+ return {};
5
+ }
6
+ const obj = {};
7
+ while (result.length >= 2) {
8
+ const streamId = result.shift();
9
+ const entries = result.shift();
10
+ if (!(streamId in obj)) {
11
+ obj[streamId] = {};
12
+ }
13
+ while (entries.length >= 2) {
14
+ const field = entries.shift();
15
+ const value = entries.shift();
16
+ try {
17
+ obj[streamId][field] = JSON.parse(value);
18
+ }
19
+ catch {
20
+ obj[streamId][field] = value;
21
+ }
22
+ }
23
+ }
24
+ return obj;
25
+ }
26
+ export class XRangeCommand extends Command {
27
+ constructor([key, start, end, count], opts) {
28
+ const command = ["XRANGE", key, start, end];
29
+ if (typeof count === "number") {
30
+ command.push("COUNT", count);
31
+ }
32
+ super(command, {
33
+ deserialize: (result) => deserialize(result[0]),
34
+ ...opts,
35
+ });
36
+ }
37
+ }
package/esm/pkg/http.js CHANGED
@@ -100,10 +100,17 @@ export class HttpClient {
100
100
  }
101
101
  const body = (await res.json());
102
102
  if (!res.ok) {
103
- throw new UpstashError(body.error);
103
+ throw new UpstashError(`${body.error}, command was: ${JSON.stringify(req.body)}`);
104
104
  }
105
105
  if (this.options?.responseEncoding === "base64") {
106
- return Array.isArray(body) ? body.map(decode) : decode(body);
106
+ if (Array.isArray(body)) {
107
+ return body.map(({ result, error }) => ({
108
+ result: decode(result),
109
+ error,
110
+ }));
111
+ }
112
+ const result = decode(body.result);
113
+ return { result, error: body.error };
107
114
  }
108
115
  return body;
109
116
  }
@@ -134,19 +141,19 @@ function base64decode(b64) {
134
141
  }
135
142
  function decode(raw) {
136
143
  let result = undefined;
137
- switch (typeof raw.result) {
144
+ switch (typeof raw) {
138
145
  case "undefined":
139
146
  return raw;
140
147
  case "number": {
141
- result = raw.result;
148
+ result = raw;
142
149
  break;
143
150
  }
144
151
  case "object": {
145
- if (Array.isArray(raw.result)) {
146
- result = raw.result.map((v) => typeof v === "string"
152
+ if (Array.isArray(raw)) {
153
+ result = raw.map((v) => typeof v === "string"
147
154
  ? base64decode(v)
148
155
  : Array.isArray(v)
149
- ? v.map(base64decode)
156
+ ? v.map(decode)
150
157
  : v);
151
158
  }
152
159
  else {
@@ -157,11 +164,11 @@ function decode(raw) {
157
164
  break;
158
165
  }
159
166
  case "string": {
160
- result = raw.result === "OK" ? "OK" : base64decode(raw.result);
167
+ result = raw === "OK" ? "OK" : base64decode(raw);
161
168
  break;
162
169
  }
163
170
  default:
164
171
  break;
165
172
  }
166
- return { result, error: raw.error };
173
+ return result;
167
174
  }
@@ -92,6 +92,7 @@ export class Pipeline {
92
92
  path,
93
93
  body: Object.values(this.commands).map((c) => c.command),
94
94
  }));
95
+ console.log("after req", { res });
95
96
  return res.map(({ error, result }, i) => {
96
97
  if (error) {
97
98
  throw new UpstashError(`Command ${i + 1} [ ${this.commands[i].command[0]} ] failed: ${error}`);
package/esm/pkg/redis.js CHANGED
@@ -1,4 +1,4 @@
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, JsonArrAppendCommand, JsonArrIndexCommand, JsonArrInsertCommand, JsonArrLenCommand, JsonArrPopCommand, JsonArrTrimCommand, JsonClearCommand, JsonDelCommand, JsonForgetCommand, JsonGetCommand, JsonMGetCommand, JsonNumIncrByCommand, JsonNumMultByCommand, JsonObjKeysCommand, JsonObjLenCommand, JsonRespCommand, JsonSetCommand, JsonStrAppendCommand, JsonStrLenCommand, JsonToggleCommand, JsonTypeCommand, 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";
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, JsonArrAppendCommand, JsonArrIndexCommand, JsonArrInsertCommand, JsonArrLenCommand, JsonArrPopCommand, JsonArrTrimCommand, JsonClearCommand, JsonDelCommand, JsonForgetCommand, JsonGetCommand, JsonMGetCommand, JsonNumIncrByCommand, JsonNumMultByCommand, JsonObjKeysCommand, JsonObjLenCommand, JsonRespCommand, JsonSetCommand, JsonStrAppendCommand, JsonStrLenCommand, JsonToggleCommand, JsonTypeCommand, 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, XAddCommand, XRangeCommand, 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
3
  import { Script } from "./script.js";
4
4
  import { ZMScoreCommand } from "./commands/zmscore.js";
@@ -1005,6 +1005,24 @@ export class Redis {
1005
1005
  writable: true,
1006
1006
  value: (...args) => new UnlinkCommand(args, this.opts).exec(this.client)
1007
1007
  });
1008
+ /**
1009
+ * @see https://redis.io/commands/xadd
1010
+ */
1011
+ Object.defineProperty(this, "xadd", {
1012
+ enumerable: true,
1013
+ configurable: true,
1014
+ writable: true,
1015
+ value: (...args) => new XAddCommand(args, this.opts).exec(this.client)
1016
+ });
1017
+ /**
1018
+ * @see https://redis.io/commands/xrange
1019
+ */
1020
+ Object.defineProperty(this, "xrange", {
1021
+ enumerable: true,
1022
+ configurable: true,
1023
+ writable: true,
1024
+ value: (...args) => new XRangeCommand(args, this.opts).exec(this.client)
1025
+ });
1008
1026
  /**
1009
1027
  * @see https://redis.io/commands/zadd
1010
1028
  */
package/esm/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "v1.22.1";
1
+ export const VERSION = "v1.23.1";
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": "v1.22.1",
6
+ "version": "v1.23.1",
7
7
  "description": "An HTTP/REST based Redis client built on top of Upstash REST API.",
8
8
  "repository": {
9
9
  "type": "git",
@@ -154,3 +154,5 @@ __exportStar(require("./zrevrank.js"), exports);
154
154
  __exportStar(require("./zscan.js"), exports);
155
155
  __exportStar(require("./zscore.js"), exports);
156
156
  __exportStar(require("./zunionstore.js"), exports);
157
+ __exportStar(require("./xadd.js"), exports);
158
+ __exportStar(require("./xrange.js"), exports);
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.XAddCommand = void 0;
4
+ const command_js_1 = require("./command.js");
5
+ /**
6
+ * @see https://redis.io/commands/xadd
7
+ */
8
+ class XAddCommand extends command_js_1.Command {
9
+ constructor([key, id, entries, opts], commandOptions) {
10
+ const command = ["XADD", key];
11
+ if (opts) {
12
+ if (opts.nomkStream) {
13
+ command.push("NOMKSTREAM");
14
+ }
15
+ if (opts.trim) {
16
+ command.push(opts.trim.type, opts.trim.comparison, opts.trim.threshold);
17
+ if (typeof opts.trim.limit !== "undefined") {
18
+ command.push("LIMIT", opts.trim.limit);
19
+ }
20
+ }
21
+ }
22
+ command.push(id);
23
+ // entries
24
+ Object.entries(entries).forEach(([k, v]) => {
25
+ command.push(k, v);
26
+ });
27
+ super(command, commandOptions);
28
+ }
29
+ }
30
+ exports.XAddCommand = XAddCommand;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.XRangeCommand = void 0;
4
+ const command_js_1 = require("./command.js");
5
+ function deserialize(result) {
6
+ if (result.length === 0) {
7
+ return {};
8
+ }
9
+ const obj = {};
10
+ while (result.length >= 2) {
11
+ const streamId = result.shift();
12
+ const entries = result.shift();
13
+ if (!(streamId in obj)) {
14
+ obj[streamId] = {};
15
+ }
16
+ while (entries.length >= 2) {
17
+ const field = entries.shift();
18
+ const value = entries.shift();
19
+ try {
20
+ obj[streamId][field] = JSON.parse(value);
21
+ }
22
+ catch {
23
+ obj[streamId][field] = value;
24
+ }
25
+ }
26
+ }
27
+ return obj;
28
+ }
29
+ class XRangeCommand extends command_js_1.Command {
30
+ constructor([key, start, end, count], opts) {
31
+ const command = ["XRANGE", key, start, end];
32
+ if (typeof count === "number") {
33
+ command.push("COUNT", count);
34
+ }
35
+ super(command, {
36
+ deserialize: (result) => deserialize(result[0]),
37
+ ...opts,
38
+ });
39
+ }
40
+ }
41
+ exports.XRangeCommand = XRangeCommand;
@@ -103,10 +103,17 @@ class HttpClient {
103
103
  }
104
104
  const body = (await res.json());
105
105
  if (!res.ok) {
106
- throw new error_js_1.UpstashError(body.error);
106
+ throw new error_js_1.UpstashError(`${body.error}, command was: ${JSON.stringify(req.body)}`);
107
107
  }
108
108
  if (this.options?.responseEncoding === "base64") {
109
- return Array.isArray(body) ? body.map(decode) : decode(body);
109
+ if (Array.isArray(body)) {
110
+ return body.map(({ result, error }) => ({
111
+ result: decode(result),
112
+ error,
113
+ }));
114
+ }
115
+ const result = decode(body.result);
116
+ return { result, error: body.error };
110
117
  }
111
118
  return body;
112
119
  }
@@ -138,19 +145,19 @@ function base64decode(b64) {
138
145
  }
139
146
  function decode(raw) {
140
147
  let result = undefined;
141
- switch (typeof raw.result) {
148
+ switch (typeof raw) {
142
149
  case "undefined":
143
150
  return raw;
144
151
  case "number": {
145
- result = raw.result;
152
+ result = raw;
146
153
  break;
147
154
  }
148
155
  case "object": {
149
- if (Array.isArray(raw.result)) {
150
- result = raw.result.map((v) => typeof v === "string"
156
+ if (Array.isArray(raw)) {
157
+ result = raw.map((v) => typeof v === "string"
151
158
  ? base64decode(v)
152
159
  : Array.isArray(v)
153
- ? v.map(base64decode)
160
+ ? v.map(decode)
154
161
  : v);
155
162
  }
156
163
  else {
@@ -161,11 +168,11 @@ function decode(raw) {
161
168
  break;
162
169
  }
163
170
  case "string": {
164
- result = raw.result === "OK" ? "OK" : base64decode(raw.result);
171
+ result = raw === "OK" ? "OK" : base64decode(raw);
165
172
  break;
166
173
  }
167
174
  default:
168
175
  break;
169
176
  }
170
- return { result, error: raw.error };
177
+ return result;
171
178
  }
@@ -95,6 +95,7 @@ class Pipeline {
95
95
  path,
96
96
  body: Object.values(this.commands).map((c) => c.command),
97
97
  }));
98
+ console.log("after req", { res });
98
99
  return res.map(({ error, result }, i) => {
99
100
  if (error) {
100
101
  throw new error_js_1.UpstashError(`Command ${i + 1} [ ${this.commands[i].command[0]} ] failed: ${error}`);
@@ -1008,6 +1008,24 @@ class Redis {
1008
1008
  writable: true,
1009
1009
  value: (...args) => new mod_js_1.UnlinkCommand(args, this.opts).exec(this.client)
1010
1010
  });
1011
+ /**
1012
+ * @see https://redis.io/commands/xadd
1013
+ */
1014
+ Object.defineProperty(this, "xadd", {
1015
+ enumerable: true,
1016
+ configurable: true,
1017
+ writable: true,
1018
+ value: (...args) => new mod_js_1.XAddCommand(args, this.opts).exec(this.client)
1019
+ });
1020
+ /**
1021
+ * @see https://redis.io/commands/xrange
1022
+ */
1023
+ Object.defineProperty(this, "xrange", {
1024
+ enumerable: true,
1025
+ configurable: true,
1026
+ writable: true,
1027
+ value: (...args) => new mod_js_1.XRangeCommand(args, this.opts).exec(this.client)
1028
+ });
1011
1029
  /**
1012
1030
  * @see https://redis.io/commands/zadd
1013
1031
  */
package/script/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = "v1.22.1";
4
+ exports.VERSION = "v1.23.1";
@@ -138,3 +138,5 @@ export * from "./zrevrank.js";
138
138
  export * from "./zscan.js";
139
139
  export * from "./zscore.js";
140
140
  export * from "./zunionstore.js";
141
+ export * from "./xadd.js";
142
+ export * from "./xrange.js";
@@ -0,0 +1,31 @@
1
+ import { Command, CommandOptions } from "./command.js";
2
+ type XAddCommandOptions = {
3
+ nomkStream?: boolean;
4
+ trim?: ({
5
+ type: "MAXLEN" | "maxlen";
6
+ threshold: number;
7
+ } | {
8
+ type: "MINID" | "minid";
9
+ threshold: string;
10
+ }) & ({
11
+ comparison: "~";
12
+ limit?: number;
13
+ } | {
14
+ comparison: "=";
15
+ limit?: never;
16
+ });
17
+ };
18
+ /**
19
+ * @see https://redis.io/commands/xadd
20
+ */
21
+ export declare class XAddCommand extends Command<string, string> {
22
+ constructor([key, id, entries, opts]: [
23
+ key: string,
24
+ id: "*" | string,
25
+ entries: {
26
+ [field: string]: unknown;
27
+ },
28
+ opts?: XAddCommandOptions
29
+ ], commandOptions?: CommandOptions<string, string>);
30
+ }
31
+ export {};
@@ -0,0 +1,9 @@
1
+ import { Command, CommandOptions } from "./command.js";
2
+ export declare class XRangeCommand<TData extends Record<string, Record<string, unknown>>> extends Command<string[][], TData> {
3
+ constructor([key, start, end, count]: [
4
+ key: string,
5
+ start: string,
6
+ end: string,
7
+ count?: number
8
+ ], opts?: CommandOptions<unknown[], TData[]>);
9
+ }
@@ -562,6 +562,31 @@ export declare class Redis {
562
562
  * @see https://redis.io/commands/unlink
563
563
  */
564
564
  unlink: (...args: CommandArgs<typeof UnlinkCommand>) => Promise<number>;
565
+ /**
566
+ * @see https://redis.io/commands/xadd
567
+ */
568
+ xadd: (key: string, id: string, entries: {
569
+ [field: string]: unknown;
570
+ }, opts?: {
571
+ nomkStream?: boolean | undefined;
572
+ trim?: (({
573
+ type: "MAXLEN" | "maxlen";
574
+ threshold: number;
575
+ } | {
576
+ type: "MINID" | "minid";
577
+ threshold: string;
578
+ }) & ({
579
+ comparison: "~";
580
+ limit?: number | undefined;
581
+ } | {
582
+ comparison: "=";
583
+ limit?: undefined;
584
+ })) | undefined;
585
+ } | undefined) => Promise<string>;
586
+ /**
587
+ * @see https://redis.io/commands/xrange
588
+ */
589
+ xrange: (key: string, start: string, end: string, count?: number | undefined) => Promise<Record<string, Record<string, unknown>>>;
565
590
  /**
566
591
  * @see https://redis.io/commands/zadd
567
592
  */
@@ -1 +1 @@
1
- export declare const VERSION = "v1.22.1";
1
+ export declare const VERSION = "v1.23.1";