MCPcopy
hub / github.com/redis/node-redis / encodeCommand

Function encodeCommand

packages/client/lib/RESP/encoder.ts:5–28  ·  view source on GitHub ↗
(args: ReadonlyArray<RedisArgument>)

Source from the content-addressed store, hash-verified

3const CRLF = '\r\n';
4
5export default function encodeCommand(args: ReadonlyArray<RedisArgument>): ReadonlyArray<RedisArgument> {
6 const toWrite: Array<RedisArgument> = [];
7
8 let strings = '*' + args.length + CRLF;
9
10 for (let i = 0; i < args.length; i++) {
11 const arg = args[i];
12 if (typeof arg === 'string') {
13 strings += '$' + Buffer.byteLength(arg) + CRLF + arg + CRLF;
14 } else if (arg instanceof Buffer) {
15 toWrite.push(
16 strings + '$' + arg.length.toString() + CRLF,
17 arg
18 );
19 strings = CRLF;
20 } else {
21 throw new TypeError(`"arguments[${i}]" must be of type "string | Buffer", got ${typeof arg} instead.`);
22 }
23 }
24
25 toWrite.push(strings);
26
27 return toWrite;
28}

Callers 2

commandsToWriteMethod · 0.85
encoder.spec.tsFile · 0.85

Calls 2

toStringMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected