From 962fd6454d876f4d97e2fed817354a889839a100 Mon Sep 17 00:00:00 2001 From: chronark Date: Tue, 10 Oct 2023 15:10:54 +0200 Subject: [PATCH 1/2] chore: remove console.log --- pkg/pipeline.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/pipeline.ts b/pkg/pipeline.ts index 7d722585..7f675d26 100644 --- a/pkg/pipeline.ts +++ b/pkg/pipeline.ts @@ -237,7 +237,6 @@ export class Pipeline[] = []> { body: Object.values(this.commands).map((c) => c.command), })) as UpstashResponse[]; - console.log("after req", { res }); return res.map(({ error, result }, i) => { if (error) { throw new UpstashError( From 7baf574281b064b10eff2aa01841f65bad24558f Mon Sep 17 00:00:00 2001 From: chronark Date: Tue, 10 Oct 2023 17:48:31 +0200 Subject: [PATCH 2/2] fix: deserialisation of xrange results --- pkg/commands/xadd.test.ts | 7 +++++-- pkg/commands/xrange.test.ts | 23 +++++++++++++++++++++++ pkg/commands/xrange.ts | 35 +++++++++++++++++------------------ 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/pkg/commands/xadd.test.ts b/pkg/commands/xadd.test.ts index eafb9374..1e8864db 100644 --- a/pkg/commands/xadd.test.ts +++ b/pkg/commands/xadd.test.ts @@ -1,4 +1,7 @@ -import { assert } from "https://deno.land/std@0.177.0/testing/asserts.ts"; +import { + assert, + assertEquals, +} from "https://deno.land/std@0.177.0/testing/asserts.ts"; import { keygen, newHttpClient, randomID } from "../test-utils.ts"; import { afterAll } from "https://deno.land/std@0.177.0/testing/bdd.ts"; @@ -120,6 +123,6 @@ Deno.test("with threshold", async (t) => { ]).exec(client); const xrangeRes = await new XRangeCommand([key, "-", "+"]).exec(client); - assert(Object.keys(xrangeRes).length === 1); + assertEquals(Object.keys(xrangeRes).length, 2); }); }); diff --git a/pkg/commands/xrange.test.ts b/pkg/commands/xrange.test.ts index 43606837..3e64502d 100644 --- a/pkg/commands/xrange.test.ts +++ b/pkg/commands/xrange.test.ts @@ -50,3 +50,26 @@ Deno.test("limit", async (t) => { }); }); }); + +Deno.test("many fields", async (t) => { + await t.step("returns all fields", async () => { + const key = newKey(); + + const fields: Record = {}; + + for (let i = 1; i <= 10; i++) { + const field = randomID(); + const value = randomID(); + fields[field] = value; + const id = await new XAddCommand([ + key, + "*", + fields, + ]).exec(client); + + const res = await new XRangeCommand([key, "-", "+"]).exec(client); + assertEquals(Object.keys(res).length, i); + assertEquals(res[id], fields); + } + }); +}); diff --git a/pkg/commands/xrange.ts b/pkg/commands/xrange.ts index df42acb3..f142c10e 100644 --- a/pkg/commands/xrange.ts +++ b/pkg/commands/xrange.ts @@ -1,27 +1,26 @@ import { Command, CommandOptions } from "./command.ts"; function deserialize>>( - result: (string | string[])[], + result: [string, string[]][], ): TData { - if (result.length === 0) { - return {} as TData; - } const obj: Record> = {}; - while (result.length >= 2) { - const streamId = result.shift() as string; - const entries = result.shift()!; + for (const e of result) { + while (e.length >= 2) { + const streamId = e.shift() as string; + const entries = e.shift()!; - if (!(streamId in obj)) { - obj[streamId] = {}; - } - while (entries.length >= 2) { - const field = (entries as string[]).shift()! as string; - const value = (entries as string[]).shift()! as string; + if (!(streamId in obj)) { + obj[streamId] = {}; + } + while (entries.length >= 2) { + const field = (entries as string[]).shift()! as string; + const value = (entries as string[]).shift()! as string; - try { - obj[streamId][field] = JSON.parse(value); - } catch { - obj[streamId][field] = value; + try { + obj[streamId][field] = JSON.parse(value); + } catch { + obj[streamId][field] = value; + } } } } @@ -48,7 +47,7 @@ export class XRangeCommand< command.push("COUNT", count); } super(command, { - deserialize: (result) => deserialize(result[0] as any), + deserialize: (result) => deserialize(result as any), ...opts, }); }