Skip to content

Instantly share code, notes, and snippets.

@qingant
Created March 25, 2020 17:10
Show Gist options
  • Save qingant/fd6ec8088fbfa66dc7b9a9fffeeee3a6 to your computer and use it in GitHub Desktop.
Save qingant/fd6ec8088fbfa66dc7b9a9fffeeee3a6 to your computer and use it in GitHub Desktop.
const _encode = (value: RedisValue): Uint8Array | Uint8Array[] => {
switch (value.tag) {
case "RedisNumber":
const v = `:${value.value}\r\n`;
return stringToBuffer(v);
case "RedisError":
return packRedisString(value.value, "-");
case "RedisString":
return packRedisString(value.value, "+");
case "RedisBulkString":
return Uint8Array.from(
[
...stringToBuffer(`\$${value.value.length}\r\n`),
...packRedisString(value.value, "$").slice(1)
]
);
case "RedisNil":
return redisNil;
case "RedisArray":
return [
stringToBuffer(`*${value.value.length}\r\n`),
...value.value.map(v => _encode(v) as Uint8Array)
];
default:
throw new FatalError(`Encode error: ${value}`);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment