Skip to content

Commit 58a197f

Browse files
authored
* wip * Update redis.ts * fixed reply types for some commands * Update .denov
1 parent 5d65f77 commit 58a197f

14 files changed

+506
-269
lines changed

.denov

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.33.0
1+
v0.34.0

io.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { BufReader, BufWriter } from "./vendor/https/deno.land/std/io/bufio.ts";
22
import Buffer = Deno.Buffer;
33
import { ErrorReplyError } from "./errors.ts";
44

5+
export type BulkResult = string | undefined;
56
export type RedisRawReply =
67
| ["status", string]
78
| ["integer", number]
8-
| ["bulk", string]
9+
| ["bulk", BulkResult]
910
| ["array", any[]]
1011
| ["error", ErrorReplyError];
1112

@@ -39,7 +40,7 @@ export async function sendCommand(
3940
writer: BufWriter,
4041
reader: BufReader,
4142
command: string,
42-
...args
43+
...args: (number | string)[]
4344
): Promise<RedisRawReply> {
4445
const msg = createRequest(command, ...args);
4546
await writer.write(encoder.encode(msg));
@@ -64,6 +65,7 @@ export async function readReply(reader: BufReader): Promise<RedisRawReply> {
6465
case ErrorReplyCode:
6566
tryParseErrorReply(await readLine(reader));
6667
}
68+
throw new Error("Invalid state");
6769
}
6870

6971
export async function readLine(reader: BufReader): Promise<string> {
@@ -81,6 +83,7 @@ export async function readLine(reader: BufReader): Promise<string> {
8183
}
8284
buf[loc++] = d;
8385
}
86+
throw new Error("Invalid state");
8487
}
8588

8689
export async function readStatusReply(reader: BufReader): Promise<string> {
@@ -100,7 +103,7 @@ export async function readIntegerReply(reader: BufReader): Promise<number> {
100103
tryParseErrorReply(line);
101104
}
102105

103-
export async function readBulkReply(reader: BufReader): Promise<string> {
106+
export async function readBulkReply(reader: BufReader): Promise<BulkResult> {
104107
const line = await readLine(reader);
105108
if (line[0] !== "$") {
106109
tryParseErrorReply(line);
@@ -109,7 +112,7 @@ export async function readBulkReply(reader: BufReader): Promise<string> {
109112
const size = parseInt(sizeStr);
110113
if (size < 0) {
111114
// nil bulk reply
112-
return;
115+
return undefined;
113116
}
114117
const dest = new Uint8Array(size + 2);
115118
await reader.readFull(dest);
@@ -119,7 +122,7 @@ export async function readBulkReply(reader: BufReader): Promise<string> {
119122
export async function readArrayReply(reader: BufReader): Promise<any[]> {
120123
const line = await readLine(reader);
121124
const argCount = parseInt(line.substr(1, line.length - 3));
122-
const result = [];
125+
const result: any[] = [];
123126
for (let i = 0; i < argCount; i++) {
124127
const res = await reader.peek(1);
125128
if (res === Deno.EOF) {
@@ -143,7 +146,7 @@ export async function readArrayReply(reader: BufReader): Promise<any[]> {
143146
return result;
144147
}
145148

146-
function tryParseErrorReply(line: string) {
149+
function tryParseErrorReply(line: string): never {
147150
const code = line[0];
148151
if (code === "-") {
149152
throw new ErrorReplyError(line);

modules-lock.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"https://deno.land/std": {
3-
"version": "@v0.33.0",
3+
"version": "@v0.34.0",
44
"modules": [
55
"/util/async.ts",
66
"/testing/asserts.ts",

modules.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"https://deno.land/std": {
3-
"version": "@v0.33.0",
3+
"version": "@v0.34.0",
44
"modules": [
55
"/util/async.ts",
66
"/testing/asserts.ts",

pipeline.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { deferred, Deferred } from "./vendor/https/deno.land/std/util/async.ts";
66

77
const encoder = new TextEncoder();
88
export type RedisPipeline = {
9-
enqueue(command: string, ...args);
9+
enqueue(command: string, ...args: (number | string)[]): void;
1010
flush(): Promise<RedisRawReply[]>;
1111
} & Redis;
1212

@@ -54,11 +54,11 @@ export function createRedisPipeline(
5454
}
5555

5656
const executor = {
57-
enqueue(command: string, ...args) {
57+
enqueue(command: string, ...args: (number | string)[]): void {
5858
const msg = createRequest(command, ...args);
5959
commands.push(msg);
6060
},
61-
async flush() {
61+
async flush(): Promise<RedisRawReply[]> {
6262
// wrap pipelined commands with MULTI/EXEC
6363
if (opts && opts.tx) {
6464
commands.splice(0, 0, createRequest("MULTI"));
@@ -80,6 +80,19 @@ export function createRedisPipeline(
8080
return ["status", "OK"];
8181
}
8282
};
83-
const fakeRedis = create(null, null, null, executor);
83+
const d = dummyReadWriteCloser();
84+
const fakeRedis = create(d, d, d, executor);
8485
return Object.assign(fakeRedis, executor);
8586
}
87+
88+
function dummyReadWriteCloser(): Deno.ReadWriteCloser {
89+
return {
90+
close() {},
91+
async read(p) {
92+
return 0;
93+
},
94+
async write(p) {
95+
return 0;
96+
}
97+
};
98+
}

pipeline_test.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,18 @@ test("pipeline in concurrent", async () => {
9191
"OK", //set(a)
9292
"OK", //set(b)
9393
"OK", //set(c)
94-
[["status", "OK"], ["status", "OK"], ["status", "OK"]], //flush()
94+
[
95+
["status", "OK"],
96+
["status", "OK"],
97+
["status", "OK"]
98+
], //flush()
9599
"OK", // get(a)
96100
"OK", // get(b)
97101
"OK", //get(c)
98-
[["bulk", "a"], ["bulk", "b"], ["bulk", "c"]] //flush()
102+
[
103+
["bulk", "a"],
104+
["bulk", "b"],
105+
["bulk", "c"]
106+
] //flush()
99107
]);
100108
});
101-

pubsub.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class RedisSubscriptionImpl implements RedisSubscription {
2828

2929
constructor(private writer: BufWriter, private reader: BufReader) {}
3030

31-
async psubscribe(...patterns) {
31+
async psubscribe(...patterns: string[]) {
3232
await sendCommand(this.writer, this.reader, "PSUBSCRIBE", ...patterns);
3333
for (const pat of patterns) {
3434
this.channels[pat] = true;

pubsub_test.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const addr = {
77
port: 6379
88
};
99

10-
async function wait(duration) {
10+
async function wait(duration: number) {
1111
return new Promise(resolve => {
1212
setTimeout(resolve, duration);
1313
});
@@ -27,20 +27,19 @@ test(async function testSubscribe2() {
2727
const redis = await connect(addr);
2828
const pub = await connect(addr);
2929
const sub = await redis.subscribe("subsc2");
30-
let message: RedisPubSubMessage;
3130
const p = (async function() {
3231
const it = sub.receive();
33-
message = (await it.next()).value;
32+
return (await it.next()).value;
3433
})();
3534
await pub.publish("subsc2", "wayway");
36-
await p;
35+
const message = await p;
3736
assertEquals(message, {
3837
channel: "subsc2",
3938
message: "wayway"
4039
});
4140
await sub.close();
4241
const a = await redis.get("aaa");
43-
assertEquals(a, void 0);
42+
assertEquals(a, undefined);
4443
pub.close();
4544
redis.close();
4645
});

0 commit comments

Comments
 (0)