Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basic RESP3 support (strings and integers only) #6

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { writeAll } from "https://deno.land/std@0.158.0/streams/conversion.ts";
export { BufReader } from "https://deno.land/std@0.158.0/io/buffer.ts";
export { concat } from "https://deno.land/std@0.158.0/bytes/mod.ts";
export { chunk } from "https://deno.land/std@0.158.0/collections/chunk.ts";

/** Testing */
export {
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const ERROR_PREFIX = "-";
export const INTEGER_PREFIX = ":";
export const BULK_STRING_PREFIX = "$";
export const ARRAY_PREFIX = "*";
export const MAP_PREFIX = "%";
11 changes: 10 additions & 1 deletion src/reply.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { type BufReader } from "../deps.ts";
import { type BufReader, chunk } from "../deps.ts";
import {
ARRAY_PREFIX,
BULK_STRING_PREFIX,
decoder,
ERROR_PREFIX,
INTEGER_PREFIX,
MAP_PREFIX,
SIMPLE_STRING_PREFIX,
} from "./constants.ts";

Expand Down Expand Up @@ -56,6 +57,12 @@ async function readArray(
return length === -1 ? null : await readNReplies(length, bufReader);
}

async function readMap(line: string, bufReader: BufReader) {
const length = readInteger(line) / 2;
const reply = await readNReplies(length, bufReader);
return Object.fromEntries(chunk(reply, 2));
}

/**
* Reads and processes the response line-by-line.
*
Expand All @@ -78,6 +85,8 @@ export async function readReply(bufReader: BufReader): Promise<Reply> {
return await readBulkString(line, bufReader);
case ARRAY_PREFIX:
return await readArray(line, bufReader);
case MAP_PREFIX:
return await readMap(line, bufReader);
/** No prefix */
default:
return line;
Expand Down
15 changes: 11 additions & 4 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,18 @@ Deno.test("methods", async (t) => {
done: false,
});
});
});

/** This test must be last */
await t.step("no reply", async () => {
await assertRejects(async () => await sendCommand(redisConn, ["SHUTDOWN"]));
});
Deno.test("RESP3", async () => {
await sendCommand(redisConn, ["HELLO", 3]);
});

/** This test must be last */
Deno.test(
"cleanup",
async () => {
await assertRejects(async () => await sendCommand(redisConn, ["SHUTDOWN"]));
},
);

addEventListener("unload", () => redisConn.close());