-
Notifications
You must be signed in to change notification settings - Fork 19
/
base.ts
130 lines (126 loc) · 3.61 KB
/
base.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { createConnection, Socket } from "net";
import { v4 as uuidv4 } from "uuid";
// core
import { Protocol } from "./protocol";
type callback = (err: boolean, res: any) => void;
export interface InterfaceBase {
id: string;
command(...parameters: Array<string | number>): Promise<any>;
close(): void;
on(event: "connect" | "timeout", listener: () => void): void;
on(event: "error", listener: (err: Error) => void): void;
on(event: "close", listener: (had_error: boolean) => void): void;
on(event: string, listener: (...args: any[]) => void): void;
}
export class Base implements InterfaceBase {
public id: string;
private socket: Socket;
private protocol: Protocol;
private callbacks: callback[];
private handle_connect?: () => void;
private handle_timeout?: () => void;
private handle_error?: (err: Error) => void;
private handle_close?: (had_error: boolean) => void;
constructor(
options: {
host?: string;
port?: number;
password?: string;
timeout?: number;
} = {}
) {
this.id = uuidv4();
this.socket = createConnection({
host: options.host || "127.0.0.1",
port: options.port || 6379,
});
this.protocol = new Protocol();
this.callbacks = [];
this.init();
if ("number" === typeof options.timeout) {
this.socket.setTimeout(options.timeout);
}
if ("string" === typeof options.password) {
this.auth(options.password);
}
}
public command(...parameters: Array<string | number>): Promise<any> {
return new Promise((resolve, reject) => {
this.callbacks.push((err, res) => {
err ? reject(res) : resolve(res);
});
this.socket.write(this.protocol.encode(...parameters));
});
}
public close() {
this.socket.end();
}
public on(event: "connect" | "timeout", listener: () => void): void;
public on(event: "close", listener: (had_error: boolean) => void): void;
public on(event: "error", listener: (err: Error) => void): void;
public on(event: string, listener: (...args: any[]) => void): void {
switch (event) {
case "connect":
this.handle_connect = listener;
break;
case "timeout":
this.handle_timeout = listener;
break;
case "error":
this.handle_error = listener;
break;
case "close":
this.handle_close = listener;
break;
default:
throw new Error("event not found");
}
}
private async auth(password: string) {
try {
return await this.command("AUTH", password);
} catch (error) {
this.socket.emit("error", error);
this.socket.end();
}
}
private init() {
this.socket.on("connect", () => {
if ("function" === typeof this.handle_connect) {
this.handle_connect();
}
});
this.socket.on("timeout", () => {
if ("function" === typeof this.handle_timeout) {
this.handle_timeout();
} else {
this.close();
}
});
this.socket.on("error", (err) => {
if ("function" === typeof this.handle_error) {
this.handle_error(err);
} else {
console.log("error:", err);
}
});
this.socket.on("close", (had_error: boolean) => {
if ("function" === typeof this.handle_close) {
this.handle_close(had_error);
}
});
this.socket.on("data", (data) => {
this.protocol.write(data);
while (true) {
this.protocol.parse();
if (!this.protocol.data.state) {
break;
}
(this.callbacks.shift() as callback)(
this.protocol.data.res.error,
this.protocol.data.res.data
);
}
});
}
}