From 3c71f69262a5b30f74e5d743c8b9415bbb4b1ce9 Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Wed, 23 Aug 2023 13:58:18 +0200 Subject: [PATCH] fix(use/http,use/http2,use/express,use/fastify): Check `writable` instead of `closed` before writing to response Closes #69 --- src/use/express.ts | 2 +- src/use/fastify.ts | 2 +- src/use/http.ts | 2 +- src/use/http2.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/use/express.ts b/src/use/express.ts index f0ead48..5a699d5 100644 --- a/src/use/express.ts +++ b/src/use/express.ts @@ -96,7 +96,7 @@ export function createHandler( res.once('close', body.return); for await (const value of body) { const closed = await new Promise((resolve, reject) => { - if (res.closed) { + if (!res.writable) { // response's close event might be late resolve(true); } else { diff --git a/src/use/fastify.ts b/src/use/fastify.ts index bddd6fb..6620148 100644 --- a/src/use/fastify.ts +++ b/src/use/fastify.ts @@ -98,7 +98,7 @@ export function createHandler( reply.raw.once('close', body.return); for await (const value of body) { const closed = await new Promise((resolve, reject) => { - if (reply.raw.closed) { + if (!reply.raw.writable) { // response's close event might be late resolve(true); } else { diff --git a/src/use/http.ts b/src/use/http.ts index 099ea26..c446b45 100644 --- a/src/use/http.ts +++ b/src/use/http.ts @@ -91,7 +91,7 @@ export function createHandler( res.once('close', body.return); for await (const value of body) { const closed = await new Promise((resolve, reject) => { - if (res.closed) { + if (!res.writable) { // response's close event might be late resolve(true); } else { diff --git a/src/use/http2.ts b/src/use/http2.ts index f09c06d..e455dcb 100644 --- a/src/use/http2.ts +++ b/src/use/http2.ts @@ -93,7 +93,7 @@ export function createHandler( res.once('close', body.return); for await (const value of body) { const closed = await new Promise((resolve, reject) => { - if (res.closed) { + if (!res.writable) { // response's close event might be late resolve(true); } else {