Skip to content

Commit

Permalink
Merge pull request #144 from iuioiua/fix-string-encoding
Browse files Browse the repository at this point in the history
fix: process all commands as mixed arrays
  • Loading branch information
iuioiua authored Sep 1, 2023
2 parents 21aca36 + bc36d7d commit 4e7d5fd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ checks formatting, runs the linter and runs tests.
## Comparison

Data recorded on August 28, 2023.
Data recorded on September 1, 2023.

### Benchmarks

Expand Down
64 changes: 32 additions & 32 deletions bench.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"results": [
{
"ok": {
"n": 902,
"min": 466291.0,
"max": 943333.0,
"avg": 560577.0,
"p75": 573000.0,
"p99": 736834.0,
"p995": 768792.0,
"p999": 943333.0
"n": 856,
"min": 526042.0,
"max": 972583.0,
"avg": 591031.0,
"p75": 601459.0,
"p99": 752375.0,
"p995": 801208.0,
"p999": 972583.0
}
}
]
Expand All @@ -30,14 +30,14 @@
"results": [
{
"ok": {
"n": 847,
"min": 499542.0,
"max": 1082958.0,
"avg": 597702.0,
"p75": 607208.0,
"p99": 923625.0,
"p995": 975667.0,
"p999": 1082958.0
"n": 838,
"min": 536333.0,
"max": 1197250.0,
"avg": 603983.0,
"p75": 615167.0,
"p99": 726583.0,
"p995": 938291.0,
"p999": 1197250.0
}
}
]
Expand All @@ -50,14 +50,14 @@
"results": [
{
"ok": {
"n": 823,
"min": 507208.0,
"max": 4869500.0,
"avg": 614529.0,
"p75": 624292.0,
"p99": 1364083.0,
"p995": 1967708.0,
"p999": 4869500.0
"n": 883,
"min": 502625.0,
"max": 1247750.0,
"avg": 572651.0,
"p75": 594417.0,
"p99": 735750.0,
"p995": 1014417.0,
"p999": 1247750.0
}
}
]
Expand All @@ -70,14 +70,14 @@
"results": [
{
"ok": {
"n": 583,
"min": 785208.0,
"max": 1650542.0,
"avg": 872035.0,
"p75": 889334.0,
"p99": 1182000.0,
"p995": 1303875.0,
"p999": 1650542.0
"n": 582,
"min": 794292.0,
"max": 1625708.0,
"avg": 874634.0,
"p75": 892166.0,
"p99": 1098916.0,
"p995": 1199250.0,
"p999": 1625708.0
}
}
]
Expand Down
32 changes: 8 additions & 24 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ const STREAMED_REPLY_START_DELIMITER = "?".charCodeAt(0);
const STREAMED_STRING_END_DELIMITER = ";0";
const STREAMED_AGGREGATE_END_DELIMITER = ".";

function createRawRequest(command: Command): Uint8Array {
const lines = [];
lines.push(encoder.encode(ARRAY_PREFIX_STRING + command.length + CRLF));
/**
* Transforms a command, which is an array of arguments, into an RESP request.
*
* @see {@link https://redis.io/docs/reference/protocol-spec/#send-commands-to-a-redis-server}
*/
function createRequest(command: Command): Uint8Array {
const lines = [encoder.encode(ARRAY_PREFIX_STRING + command.length + CRLF)];
for (const arg of command) {
const bytes = arg instanceof Uint8Array
? arg
Expand All @@ -53,31 +57,11 @@ function createRawRequest(command: Command): Uint8Array {
encoder.encode(BULK_STRING_PREFIX_STRING + bytes.byteLength + CRLF),
);
lines.push(bytes);
lines.push(encoder.encode(CRLF));
lines.push(CRLF_RAW);
}
return concat(...lines);
}

function createStringRequest(command: (string | number)[]): Uint8Array {
let string = ARRAY_PREFIX_STRING + command.length + CRLF;
for (const arg of command) {
string += BULK_STRING_PREFIX_STRING + arg.toString().length + CRLF;
string += arg + CRLF;
}
return encoder.encode(string);
}

/**
* Transforms a command, which is an array of arguments, into an RESP request.
*
* @see {@link https://redis.io/docs/reference/protocol-spec/#send-commands-to-a-redis-server}
*/
function createRequest(command: Command): Uint8Array {
return command.some((arg) => arg instanceof Uint8Array)
? createRawRequest(command)
: createStringRequest(command as (string | number)[]);
}

/**
* Just writes a command to the Redis server without listening for a reply.
*
Expand Down

0 comments on commit 4e7d5fd

Please sign in to comment.