fix: Avoid ERR_STREAM_WRITE_AFTER_END error
MODIFY
lib/express/idempotency.js
lib/express/idempotency.js
@@ -36,7 +36,9 @@ function cacheResponse (key, req, res) {
});
const originalSocketWriter = res.socket.write.bind(res.socket);
const patchedWriter = (chunk, encoding, next) => {
+ if (responseListener.writable) {
responseListener.write(chunk);
+ }
return originalSocketWriter(chunk, encoding, next);
};
res.socket.write = patchedWriter;
@@ -55,6 +57,10 @@ function cacheResponse (key, req, res) {
}
});
responseListener.once('close', () => {
+ if (res.socket?.write === patchedWriter) {
+ res.socket.write = originalSocketWriter;
+ }
+
if (!IDEMPOTENT_RESPONSES.has(key)) {
const msg = `Could not cache the response identified by '${key}'. ` +
`Cache consistency has been damaged`;

fix: Avoid ERR_STREAM_WRITE_AFTER_END error (#401)