-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathencoder.ts
More file actions
35 lines (29 loc) · 1.07 KB
/
encoder.ts
File metadata and controls
35 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { RedisArgument } from './types';
const CRLF = '\r\n';
export default function encodeCommand(args: ReadonlyArray<RedisArgument>): ReadonlyArray<RedisArgument> {
const toWrite: Array<RedisArgument> = [];
let strings = '*' + args.length + CRLF;
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (typeof arg === 'string') {
strings += '$' + Buffer.byteLength(arg) + CRLF + arg + CRLF;
} else if (arg instanceof Buffer) { // Check Buffer before Uint8Array since Buffer extends Uint8Array in Node.js
toWrite.push(
strings + '$' + arg.length.toString() + CRLF,
arg
);
strings = CRLF;
} else if (arg instanceof Uint8Array) {
const buffer = Buffer.from(arg.buffer, arg.byteOffset, arg.byteLength);
toWrite.push(
strings + '$' + buffer.length.toString() + CRLF,
buffer
);
strings = CRLF;
} else {
throw new TypeError(`"arguments[${i}]" must be of type "string | Buffer | Uint8Array", got ${typeof arg} instead.`);
}
}
toWrite.push(strings);
return toWrite;
}