mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
565 stars 127 forks source link

fix(ClientRequest): support intercepting buffered requests #643

Open kettanaito opened 1 month ago

kettanaito commented 1 month ago

Changes

req.on('socket', (socket) => {
  socket.on('connect', () => req.end())
})

Here, the request header won't be sent until the socket connection is established. Previously, the interceptor would hang on this forever since writing the request header was mandatory as it triggers the request parser).

kettanaito commented 1 month ago

Issue

_read is non-deterministic and is called multiple times during the socket life cycle. We cannot rely on it to check if someone has written to the socket yet. Also, writes can happen after _read, which defeats its entire purpose.

I'm trying to see what we can do to make this process more streamlined... Ideally, we can trigger onRequest on MockHttpSocket.connect. If the request listener consumes the request body, we are flushing the request header buffer anyway right now to make that read possible.