()
| 65 | } |
| 66 | |
| 67 | connect() { |
| 68 | this.socket = createServer((c) => { |
| 69 | const clientIndex = this.clients.push(c) - 1; |
| 70 | process.nextTick(() => { |
| 71 | this.emit("connect", c); |
| 72 | }); |
| 73 | |
| 74 | const parser = new Parser({ |
| 75 | returnBuffers: true, |
| 76 | stringNumbers: false, |
| 77 | returnReply: (reply: any) => { |
| 78 | reply = convertBufferToString(reply, "utf8"); |
| 79 | if ( |
| 80 | reply.length === 3 && |
| 81 | reply[0].toLowerCase() === "client" && |
| 82 | reply[1].toLowerCase() === "setname" |
| 83 | ) { |
| 84 | connectionNameMap.set(c, reply[2]); |
| 85 | } |
| 86 | if ( |
| 87 | this.slotTable && |
| 88 | reply.length === 2 && |
| 89 | reply[0].toLowerCase() === "cluster" && |
| 90 | reply[1].toLowerCase() === "slots" |
| 91 | ) { |
| 92 | this.write(c, this.slotTable); |
| 93 | return; |
| 94 | } |
| 95 | const flags: Flags = {}; |
| 96 | const handlerResult = this.handler && this.handler(reply, c, flags); |
| 97 | if (!flags.hang) { |
| 98 | this.write(c, handlerResult); |
| 99 | } |
| 100 | if (flags.disconnect) { |
| 101 | this.disconnect(); |
| 102 | } |
| 103 | }, |
| 104 | returnError: () => {}, |
| 105 | }); |
| 106 | |
| 107 | c.on("end", () => { |
| 108 | this.clients[clientIndex] = null; |
| 109 | this.emit("disconnect", c); |
| 110 | }); |
| 111 | |
| 112 | c.on("data", (data) => { |
| 113 | parser.execute(data); |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | this.socket.listen(this.port); |
| 118 | enableDestroy(this.socket); |
| 119 | } |
| 120 | |
| 121 | disconnect(callback?: () => void) { |
| 122 | // @ts-expect-error |
no test coverage detected