Open
Conversation
Member
|
Thank you for PR, looks like a nice fix. Would you be able to add one unit test? |
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
Author
Sure, where would you expect this unittest to be located - I mean in which *.test.ts file? |
Author
|
Hi @pi0 I came up with this test case. The only thing is I am not sure where to put it. It seems to me that no test file really fits :/ maybe it("can abort endless stream request", async () => {
let connectionClosed = false;
let streamCancelled = false;
app.use(
"/",
eventHandler(async (event) => {
event.node.res.setHeader(
"Content-Type",
"text/event-stream; charset=utf-8",
);
event.node.res.setHeader("Cache-Control", "no-cache");
event.node.res.setHeader("Connection", "keep-alive");
event.node.res.setHeader("X-Accel-Buffering", "no");
// Detect when client disconnects
event.node.res.on("close", () => {
connectionClosed = true;
});
// clean up the stream
let intervalId;
const encoder = new TextEncoder();
const stream = new ReadableStream({
start(controller) {
intervalId = setInterval(() => {
controller.enqueue(encoder.encode("data: ping...\n\n"));
}, 10);
},
// this is called when the stream has been cancelled trouhgh the AbortController signal in the sendStream function
// commenting out the part of abort signal in the sendStream function => this will not be called after the request is cancelled
cancel() {
streamCancelled = true;
// clean up the stream
clearInterval(intervalId);
},
});
return sendStream(event, stream);
}),
);
const res = await fetch(url + "/", {
method: "GET",
headers: {
Accept: "text/event-stream",
},
});
if (!res.body) {
throw new Error("No response body");
} else {
let messagesCounter = 0;
const abort = new AbortController();
res.body
.pipeTo(
new WritableStream({
write(chunk) {
messagesCounter += 1;
if (messagesCounter > 5) {
// abort reading of the stream after few chunks
// simulate client disconnect
abort.abort();
}
},
}),
{ signal: abort.signal },
)
// hanlde the error that is thrown when the stream is aborted
.catch((err) => {});
}
await new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, 500);
});
expect(res.status).toEqual(200);
expect(connectionClosed !== false).toBe(true);
expect(streamCancelled !== false).toBe(true);
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
resolves #1045
Implements the fix proposed in #1045. Enables handling of aborted (endless) streams e.g. proxied SSE streams.
Note:
This is my first contribution, feel free to suggest improvements of any kind 😊