-
Notifications
You must be signed in to change notification settings - Fork 225
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
fix: ensure correct streaming when using mimic-response #429
Conversation
The `mimic-response` module exposes a single function with the following API: mimicResponse(fromStream, toStream) If someone is calling `mimicResponse` where the `fromStream` have had its emitter functions bound using `bindEmitter(fromStream)`, the `toStream` will wrongly bind all its event listeners to the `fromStream`. This becomes especially problematic in case the data is transformed either in the `toStream` or in between `fromStream` and `toStream`, eg: fromStream.pipe(zlib.createGunzip()).pipe(toStream) In those cases, the data emitted by `toStream` will actually be the un-transformed data coming from the `fromStream`. This commit patches `mimic-response` so that the event emitters of `toStream` runs in the expected context. Fixes elastic#423
Btw, I discovered that this isn't the first time we've had issues with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another test for the mimic-response patches would be to verify a transformed stream is transformed as expected. A simple upper-casing transform would work.
What you have here seems sufficient though, if you want to merge and release right away and expand tests in another PR?
// Start simple server that performs got-request on every request | ||
var server = http.createServer(function (req, res) { | ||
got(url).then(function (response) { | ||
t.equal(response.body[0], '\'', 'body should be uncompressed') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd verify more than just the first character.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's gzipped, it will always start with a few magic gzip bytes, so it can never be '
. But I could just read the entire file into memory up top and do a complete comparison - that way it's good for sure 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm actually the downside of that is that the test output will display the binary gzipped data if the test fails when mimic-response isn't correctly patched. And that might mess with your shell. The first magic byte in the gzip header is \x1f
and will always be displayed as such, so there's no danger of messing up the terminal in that case.
Here's how it looks if I display it all:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Continuing the conversation with my self: Actually the dangerous bytes are probably all escaped - just as \x1f
, so I guess my concern is invalid 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, ended up testing just the first line and added a test for the total length as well. That should hopefully cover everything 😃
The mechanism to see if an EventEmitter has already been bound to a run context (as required by the mimic-response instrumentation; and *only* it) has changed. This also removes the shimmer.isWrapped() export that was added in #429 just for this case and is still the only usage of it.
The
mimic-response
module exposes a single function with the following API:If someone is calling
mimicResponse
where thefromStream
have had its emitter functions bound usingbindEmitter(fromStream)
, thetoStream
will wrongly bind all its event listeners to thefromStream
.This becomes especially problematic in case the data is transformed either in the
toStream
or in betweenfromStream
andtoStream
, eg:In those cases, the data emitted by
toStream
will actually be the un-transformed data coming from thefromStream
.This commit patches
mimic-response
so that the event emitters oftoStream
runs in the expected context.Fixes #423