-
Notifications
You must be signed in to change notification settings - Fork 549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[diagnostics_channel] support body received? #1342
Comments
You can't clone a node stream. Here is a module that implements that functionality in case you need it: https://www.npmjs.com/package/cloneable-readable. Overall everything that improves diagnostics_channel is great. However that's a low level instrument, so cloning would be off the table. |
I think with a bodySent property it could be left to the user to use your package... although that might not be the greatest UX? |
Would you mind to assemble a prototype? You have definitely researched this more than me. |
Exposing the ReadableStream body can be done like so (not cloned) diff --git a/lib/core/request.js b/lib/core/request.js
index f04fe4f..8bbe4e7 100644
--- a/lib/core/request.js
+++ b/lib/core/request.js
@@ -216,7 +216,10 @@ class Request {
this.completed = true
if (channels.trailers.hasSubscribers) {
- channels.trailers.publish({ request: this, trailers })
+ channels.trailers.publish({
+ request: { ...this, bodyReceived: this[kHandler].res },
+ trailers
+ })
}
return this[kHandler].onComplete(trailers)
} Unsure if it's even useful as it probably would suffer from race conditions (consuming the body somewhere else before cloning)... I'll look into it more |
Found a much better way to do this, I'll make a PR soon to see what you think. |
undici:request:trailers
Is there any update regarding this? If not, is there any alternative solution without modifying undici or a package source code? I would like to see the sent and received body for debugging, as error messages returned by other packages usually don't include the full body. |
You can always use |
I don't see how this would help me as I would still have to modify the source code to add the interceptor there, unless you're able to apply this globally which, if possible, I haven't been able to figure out. |
You can apply it by creating a custom |
Looks good, but as I have to specify an URL/domain when creating the Client, using this breaks all requests to URLs that aren't on the same domain as the one specified... However, this'll work for now, thank you so far! |
This would solve...
In a library I am working on, they had 2 events (request & response) that would fire before and after a request was made. The purpose of these events was for analytics (ie. receiving a "weird" response code and checking the body). With undici, I'm hoping that these events can be removed entirely and instead use the
diagnostics_channel
support (the library only supports v16.9.0+).The implementation should look like...
In the
request
property, the body is sent but under a symbol property and uncloned. Exposing it publicly (maybe underbodyReceived
or some other name) and cloning it so it can be used twice would be exactly what I need. Receiving a buffer would also be very useful.Since it is using
request
, rather thanfetch
, it is using a node Readable stream so we can't just tee the stream.I have also considered...
Additional context
The implementation previously used was not performant and didn't need to be because it was only supposed to be used when something majorly messed up.
The text was updated successfully, but these errors were encountered: