import { BufReader, concat, type ReadLineResult, writeAll } from "./deps.ts";
export type Command = (string | number | Uint8Array)[];export type Reply = string | number | null | Reply[];
const CRLF = "\r\n";const encoder = new TextEncoder();const decoder = new TextDecoder();
const SIMPLE_STRING_PREFIX = "+";const ERROR_PREFIX = "-";const INTEGER_PREFIX = ":";const BULK_STRING_PREFIX = "$";const ARRAY_PREFIX = "*";
function removePrefix(line: string): string { return line.slice(1);}
async function readLine(bufReader: BufReader): Promise<ReadLineResult> { return await bufReader.readLine() ?? await Promise.reject("No reply received from Redis server");}
function createRequest(command: Command): Uint8Array { const parts: Uint8Array[] = []; parts.push(encoder.encode(ARRAY_PREFIX + command.length + CRLF)); for (const arg of command) { const bytes = arg instanceof Uint8Array ? arg : encoder.encode(arg.toString()); parts.push(encoder.encode(BULK_STRING_PREFIX + bytes.byteLength + CRLF)); parts.push(bytes); parts.push(encoder.encode(CRLF)); } return concat(...parts);}
export async function writeCommand( redisConn: Deno.Conn, command: Command,): Promise<void> { await writeAll(redisConn, createRequest(command));}
async function readReply(bufReader: BufReader): Promise<Reply> { const result = await readLine(bufReader); const line = decoder.decode(result.line); switch (line.charAt(0)) { case SIMPLE_STRING_PREFIX: return removePrefix(line); case ERROR_PREFIX: return await Promise.reject(removePrefix(line)); case INTEGER_PREFIX: return Number(removePrefix(line)); case BULK_STRING_PREFIX: return Number(removePrefix(line)) === -1 ? null : await readReply(bufReader); case ARRAY_PREFIX: { const length = Number(removePrefix(line)); if (length === -1) { return null; } const array: Reply[] = []; for (let i = 0; i < length; i++) { array.push(await readReply(bufReader)); } return array; } default: return line; }}
export async function sendCommand( redisConn: Deno.Conn, command: Command,): Promise<Reply> { await writeCommand(redisConn, command); return await readReply(new BufReader(redisConn));}
async function readRawReply(bufReader: BufReader): Promise<Uint8Array> { const result = await readLine(bufReader); return decoder.decode(result.line).startsWith(BULK_STRING_PREFIX) ? (await readLine(bufReader))!.line : await Promise.reject("Reply must be a bulk string");}
export async function sendCommandRawReply( redisConn: Deno.Conn, command: Command,): Promise<Uint8Array> { await writeCommand(redisConn, command); return await readRawReply(new BufReader(redisConn));}
export async function pipelineCommands( redisConn: Deno.Conn, commands: Command[],): Promise<Reply[]> { const request = concat(...commands.map(createRequest)); await writeAll(redisConn, request); const bufReader = new BufReader(redisConn); const replies: Reply[] = []; for (let i = 0; i < commands.length; i++) { replies.push(await readReply(bufReader)); } return replies;}
export async function* listenReplies( redisConn: Deno.Conn,): AsyncIterableIterator<Reply> { const bufReader = new BufReader(redisConn); while (true) { yield await readReply(bufReader); }}