-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.ts
90 lines (80 loc) · 2.01 KB
/
protocol.ts
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { stringToBuffer, show } from "./encoder.ts";
export * from "./encoder.ts";
export interface RedisString {
tag: "RedisString";
value: Uint8Array;
} // binary safe byte string
export interface RedisBulkString {
tag: "RedisBulkString";
value: Uint8Array;
}
export interface RedisNumber {
tag: "RedisNumber";
value: number;
}
export interface RedisError {
tag: "RedisError";
value: Uint8Array;
}
export interface RedisNil {
tag: "RedisNil";
}
export type ArrayItem = RedisString | RedisNumber | RedisError
| RedisBulkString;
export interface RedisArray {
tag: "RedisArray";
value: ArrayItem[];
}
export type RedisValue = RedisString | RedisNumber | RedisError | RedisNil
| RedisArray | RedisBulkString;
const RedisNilValue: RedisValue = {
tag: "RedisNil"
};
const RedisOk: RedisValue = {
tag: "RedisString",
value: stringToBuffer("Ok")
};
export const RedisValueOf = {
number: (n: number): RedisValue => {
return { tag: "RedisNumber", value: n };
},
string: (s: string): RedisBulkString => {
return { tag: "RedisBulkString", value: stringToBuffer(s) };
},
array: (ss: string[]): RedisArray => {
return { tag: "RedisArray", value: ss.map(v => RedisValueOf.string(v)) };
},
error: (error: string): RedisError => {
return { tag: "RedisError", value: stringToBuffer(error) };
},
nil: RedisNilValue,
ok: RedisOk
};
if (import.meta.main) {
const err: RedisValue = {
tag: "RedisError",
value: stringToBuffer("这是一个错误, this is an error")
};
const num: RedisValue = {
tag: "RedisNumber",
value: 1024
};
const arr: RedisArray = {
tag: "RedisArray",
value: [
RedisOk,
{
tag: "RedisString",
value: stringToBuffer("a string is a string")
} as RedisString
]
};
const bulkStr: RedisBulkString = {
tag: "RedisBulkString",
value: stringToBuffer("This is a bulk string")
};
[RedisOk, bulkStr, RedisNilValue, err, num, arr].forEach(v => {
console.log(v);
console.log(show(v));
});
}