Skip to content

fix: proper fetching of binary data during get requests#11

Merged
mjwwit merged 6 commits intomjwwit:masterfrom
RobertMirandola:fetch-binary-data
Oct 12, 2022
Merged

fix: proper fetching of binary data during get requests#11
mjwwit merged 6 commits intomjwwit:masterfrom
RobertMirandola:fetch-binary-data

Conversation

@RobertMirandola
Copy link

@RobertMirandola RobertMirandola commented Sep 26, 2022

No description provided.

@RobertMirandola
Copy link
Author

This MR allows for synchronous and asynchronous GET requests that fetch binary data to be returned in a new field called XMLHttpRequest.response with the expected binary data. XMLHttpRequest.responseText is still text that is encoded in 'utf8' format.

@RobertMirandola
Copy link
Author

RobertMirandola commented Sep 26, 2022

The original MR for this was wrriten by @georgestagg and was written to be merged into a forked version of this repo by @driverdan: driverdan@d251dae

The reason I am re opening the MR here is because there were some merge conficts between this repo and the other forked version by @driverdan that did not seem like they were worth resolving. Also it doesn't seem like that forked repo is being maintained given the time of the last commit.

@mjwwit
Copy link
Owner

mjwwit commented Sep 28, 2022

Thanks for this! The inactivity of the original repository and other forks of it was the reasoning behind starting this one. As the package built from this repository is depended on by a large number of other packages I'll have to test it thoroughly, so please bear with me.

@RobertMirandola
Copy link
Author

@mjwwit Just wanted to let you know that I just added an extra commit dealing with local files containing binary data as well as support for this.response within this.abort()

Copy link
Owner

@mjwwit mjwwit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still some work to be done on this. Besides the comments there should also be a test file for this.

} else {
self.status = 200;
self.responseText = data;
self.response = Buffer.from(fs.readFileSync(unescape(url.pathname), 'binary'), 'binary');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this does what you want, binary encoding in Node.js is an alias for latin1. Also, first encoding the file buffer to binary only to then decode it again seems quite silly.

} else {
try {
this.responseText = fs.readFileSync(unescape(url.pathname), 'utf8');
this.response = Buffer.from(fs.readFileSync(unescape(url.pathname), 'binary'), 'binary');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're now effectively duplicating the work. I'd prefer a way where we can use the result of one readFileSync to provide both responseText and response with data.


if (response && response.setEncoding) {
response.setEncoding("utf8");
response.setEncoding('binary');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this is probably not what you think.

…eading from local files to only happen once in order to attain response and responseText
@RobertMirandola
Copy link
Author

@mjwwit I've read your changes and made a commit in response. I did not know that setting binary to the setEncoding function was an alias to latin1. So I instead completely removed response.setEncoding() so that I could receive the raw data in chunks. I just create a buffer now with that raw data with no encoding specified to properly read the binary data. The responseText stays the same, as it always has (a toString() with utf8 encoding). Also, for local files, I got rid of the buffer creation and instead just read the file without any encodings at all to get the correct data for this.response, and the encoded the string, this.responseText, to 'utf8' encoding, as was done before. The duplication of work is now eliminated as well.

Thanks and I look forward to your thoughts!

@RobertMirandola
Copy link
Author

@mjwwit I also just commited a test file as well

@mjwwit
Copy link
Owner

mjwwit commented Oct 11, 2022

That is tons better! The only thing missing now is a reference to the new file (and the renamed one) in the package.json test script. If you add that I think we're good for a merge.
I'm surprised you got through the code, even for very old code it's absolute garbage. Who knows, maybe I'll rewrite it properly some day...

@RobertMirandola
Copy link
Author

@mjwwit Done! Thanks so much for taking the time to review this PR, some of the work we do involves downloading raw binary data so trying to find a fix for this repo was unfortunately very necessary since we do use it😆

@mjwwit
Copy link
Owner

mjwwit commented Oct 12, 2022

I found a last problem in the code, but it's not (and was never) covered by any tests. As this is not really your problem I'll fix this myself after the merge.

@mjwwit mjwwit merged commit 8514b4e into mjwwit:master Oct 12, 2022
@georgestagg
Copy link

Thanks for your further work on this @RobertMirandola & @mjwwit and for getting this change merged. This will be very useful for me too and I look forward to being able to switch from my fork back to a proper npm package!

@mjwwit
Copy link
Owner

mjwwit commented Oct 12, 2022

I've released version 2.1.0 with your PR, my fix (and a test to verify)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants