Skip to content

Commit a79a62d

Browse files
committed
--wip-- [skip ci]
1 parent d7c6544 commit a79a62d

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

packages/test-utils/lib/redis-proxy.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ export class RedisProxy extends EventEmitter {
5454
public readonly config: Required<ProxyConfig>;
5555
private readonly connections: Map<string, ActiveConnection>;
5656
private isRunning: boolean;
57+
private transformations: ((data: Buffer) => Buffer)[] = [];
5758

5859
constructor(config: ProxyConfig) {
5960
super();
6061

61-
6262
this.config = {
6363
listenHost: '127.0.0.1',
6464
timeout: 30000,
@@ -113,6 +113,10 @@ export class RedisProxy extends EventEmitter {
113113
});
114114
}
115115

116+
public setTransformations(transformations: ((data: Buffer) => Buffer)[]): void {
117+
this.transformations = transformations;
118+
}
119+
116120
public getStats(): ProxyStats {
117121
const connections = Array.from(this.connections.values());
118122

@@ -217,6 +221,11 @@ export class RedisProxy extends EventEmitter {
217221
});
218222
}
219223

224+
private applyTransformations(data: Buffer): Buffer {
225+
if(!this.transformations.length) return data
226+
return this.transformations.reduce((acc, fn) => fn(acc), data);
227+
}
228+
220229
private handleClientConnection(clientSocket: net.Socket): void {
221230
const connectionId = this.generateConnectionId();
222231
const serverSocket = net.createConnection({
@@ -249,8 +258,9 @@ export class RedisProxy extends EventEmitter {
249258
});
250259

251260
serverSocket.on('data', (data) => {
252-
this.emit('data', connectionId, 'server->client', data);
253-
clientSocket.write(data);
261+
const transformedData = this.applyTransformations(data);
262+
this.emit('data', connectionId, 'server->client', transformedData);
263+
clientSocket.write(transformedData);
254264
});
255265

256266
clientSocket.on('close', () => {
@@ -326,4 +336,3 @@ export function getFreePortNumber(): Promise<number> {
326336

327337
export { RedisProxy as RedisTransparentProxy };
328338
export type { ProxyConfig, ConnectionInfo, ProxyEvents, SendResult, DataDirection, ProxyStats };
329-

0 commit comments

Comments
 (0)