Skip to content

Commit

Permalink
fix: remove leaky readLines() and readReply() functions (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua authored Dec 19, 2024
1 parent 001a888 commit 8b19699
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
18 changes: 2 additions & 16 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,7 @@ async function writeCommand(

const DELIM_LPS = new Uint8Array([0, 0]);

/**
* Reads and processes the response line-by-line. Exported for testing.
*
* @private
*/
export async function* readLines(
reader: Reader,
): AsyncIterableIterator<Uint8Array> {
async function* readLines(reader: Reader): AsyncIterableIterator<Uint8Array> {
let chunks = new Uint8Array();

// Modified KMP
Expand Down Expand Up @@ -148,14 +141,7 @@ function readNReplies(
return Array.fromAsync({ length }, () => readReply(iterator, raw));
}

/**
* Reads and processes the response reply-by-reply. Exported for testing.
*
* @see {@link https://github.com/redis/redis-specifications/blob/master/protocol/RESP3.md}
*
* @private
*/
export async function readReply(
async function readReply(
iterator: AsyncIterableIterator<Uint8Array>,
raw = false,
): Promise<Reply> {
Expand Down
18 changes: 6 additions & 12 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { assertEquals, assertRejects } from "@std/assert";
import { Buffer } from "@std/io/buffer";
import {
type Command,
readLines,
readReply,
RedisClient,
type Reply,
} from "./mod.ts";
import { type Command, RedisClient, type Reply } from "./mod.ts";

const encoder = new TextEncoder();

async function readReplyTest(output: string, expected: Reply) {
assertEquals(
await readReply(readLines(new Buffer(encoder.encode(output)))),
expected,
);
const redisClient = new RedisClient(new Buffer(encoder.encode(output)));
const { value } = await redisClient.readReplies().next();
assertEquals(value, expected);
}

function readReplyRejectTest(output: string, expected: string) {
const redisClient = new RedisClient(new Buffer(encoder.encode(output)));
return assertRejects(
() => readReply(readLines(new Buffer(encoder.encode(output)))),
() => redisClient.readReplies().next(),
expected,
);
}
Expand Down

0 comments on commit 8b19699

Please sign in to comment.