You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-12-15 23:55:38 +03:00
chore: proxy improvements (#3121)
* introduce global interceptors * move proxy stuff to new folder * implement resp framer * properly handle request/response and push * add global interceptor
This commit is contained in:
committed by
GitHub
parent
96a8a847f6
commit
130e88d45c
43
packages/test-utils/lib/proxy/resp-queue.ts
Normal file
43
packages/test-utils/lib/proxy/resp-queue.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { EventEmitter } from "node:events";
|
||||
import RespFramer from "./resp-framer";
|
||||
import { Socket } from "node:net";
|
||||
|
||||
interface Request {
|
||||
resolve: (data: Buffer) => void;
|
||||
reject: (reason: any) => void;
|
||||
}
|
||||
|
||||
export default class RespQueue extends EventEmitter {
|
||||
queue: Request[] = [];
|
||||
respFramer: RespFramer = new RespFramer();
|
||||
|
||||
constructor(private serverSocket: Socket) {
|
||||
super();
|
||||
this.respFramer.on("message", (msg) => this.handleMessage(msg));
|
||||
this.serverSocket.on("data", (data) => this.respFramer.write(data));
|
||||
}
|
||||
|
||||
handleMessage(data: Buffer) {
|
||||
const request = this.queue.shift();
|
||||
if (request) {
|
||||
request.resolve(data);
|
||||
} else {
|
||||
this.emit("push", data);
|
||||
}
|
||||
}
|
||||
|
||||
request(data: Buffer): Promise<Buffer> {
|
||||
let resolve: (data: Buffer) => void;
|
||||
let reject: (reason: any) => void;
|
||||
|
||||
const promise = new Promise<Buffer>((rs, rj) => {
|
||||
resolve = rs;
|
||||
reject = rj;
|
||||
});
|
||||
|
||||
//@ts-ignore
|
||||
this.queue.push({ resolve, reject });
|
||||
this.serverSocket.write(data);
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user