Skip to content

Commit

Permalink
fix(use/http,use/http2,use/express,use/fastify): Handle cases where r…
Browse files Browse the repository at this point in the history
…esponse's `close` event is late (#75)

Closes #69
  • Loading branch information
enisdenjo authored Aug 22, 2023
1 parent b7c980f commit 4457cba
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
14 changes: 11 additions & 3 deletions src/use/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,17 @@ export function createHandler<Context extends OperationContext = undefined>(

res.once('close', body.return);
for await (const value of body) {
await new Promise<void>((resolve, reject) =>
res.write(value, (err) => (err ? reject(err) : resolve())),
);
const closed = await new Promise((resolve, reject) => {
if (res.closed) {
// response's close event might be late
resolve(true);
} else {
res.write(value, (err) => (err ? reject(err) : resolve(false)));
}
});
if (closed) {
break;
}
}
res.off('close', body.return);
return new Promise((resolve) => res.end(resolve));
Expand Down
14 changes: 11 additions & 3 deletions src/use/fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,17 @@ export function createHandler<Context extends OperationContext = undefined>(

reply.raw.once('close', body.return);
for await (const value of body) {
await new Promise<void>((resolve, reject) =>
reply.raw.write(value, (err) => (err ? reject(err) : resolve())),
);
const closed = await new Promise((resolve, reject) => {
if (reply.raw.closed) {
// response's close event might be late
resolve(true);
} else {
reply.raw.write(value, (err) => (err ? reject(err) : resolve(false)));
}
});
if (closed) {
break;
}
}
reply.raw.off('close', body.return);
return new Promise((resolve) => reply.raw.end(resolve));
Expand Down
14 changes: 11 additions & 3 deletions src/use/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,17 @@ export function createHandler<Context extends OperationContext = undefined>(

res.once('close', body.return);
for await (const value of body) {
await new Promise<void>((resolve, reject) =>
res.write(value, (err) => (err ? reject(err) : resolve())),
);
const closed = await new Promise((resolve, reject) => {
if (res.closed) {
// response's close event might be late
resolve(true);
} else {
res.write(value, (err) => (err ? reject(err) : resolve(false)));
}
});
if (closed) {
break;
}
}
res.off('close', body.return);
return new Promise((resolve) => res.end(resolve));
Expand Down
14 changes: 11 additions & 3 deletions src/use/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,17 @@ export function createHandler<Context extends OperationContext = undefined>(

res.once('close', body.return);
for await (const value of body) {
await new Promise<void>((resolve, reject) =>
res.write(value, (err) => (err ? reject(err) : resolve())),
);
const closed = await new Promise((resolve, reject) => {
if (res.closed) {
// response's close event might be late
resolve(true);
} else {
res.write(value, (err) => (err ? reject(err) : resolve(false)));
}
});
if (closed) {
break;
}
}
res.off('close', body.return);
return new Promise((resolve) => res.end(resolve));
Expand Down

0 comments on commit 4457cba

Please sign in to comment.