(args: ReadonlyArray<RedisArgument>)
| 3 | const CRLF = '\r\n'; |
| 4 | |
| 5 | export 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 | } |
no test coverage detected