moll / node-mitm

Intercept and mock outgoing Node.js network TCP connections and HTTP requests for testing. Intercepts and gives you a Net.Socket, Http.IncomingMessage and Http.ServerResponse to test and respond with. Super useful when testing code that hits remote servers.
Other
637 stars 48 forks source link

Configurable delay options on the original socket #74

Open kamaz opened 2 years ago

kamaz commented 2 years ago

I was wondering how open are you for a PR that would open ability to configure a delay on the socket.

I was thinking something between these line:

Mitm.prototype.connect = function connect(orig, Socket, opts, done) {
    ...
    const origSocket = orig.call(this, opts, done)
    if (client.delay) {
      origSocket.pause()
      setTimeout(() => {
        origSocket.resume()
      }, client.delay)
    } 
    ...
}

Potential usage:

    mitm.on('connect', function (socket: any, _opts: any) {
      console.log('bypassing connection')
      socket.delay = 1000
      socket.bypass()
    })