-
Hi, in readme of this project I see this chapter: This section says that the body must be read. I have small library that makes possible to add "middleware" for async function middleware (request, next) {
doSomethingBefore();
const response = await next(request);
doSomethingAfter();
return response;
} Theoretically, I can write an middleware that adds some kind of validation of the response based on headers for example and not on body like this: async function middleware (request, next) {
const response = await next(request);
if (isInvalidHeaders(response)) {
throw Error('Validation error...')
}
return response;
} In this case, the body will not be read since the promise will go into the catch state. In real world applications there can be a lot of requests that would not pass such validations Please explain whether this approach can cause problems with memory consumption? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
It will be a problem. You can dump the body before throwing. async function middleware (request, next) {
const response = await next(request);
if (isInvalidHeaders(response)) {
dump(response)
throw Error('Validation error...')
}
return response;
}
async function dump(response) {
try {
for await (const chunk of response) {
return
}
} catch {
// Do nothing
}
} |
Beta Was this translation helpful? Give feedback.
It will be a problem. You can dump the body before throwing.