Skip to content
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

Populate XHR response property with binary data #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

georgestagg
Copy link

Always populating response is a hack (one should really request it via responseType), but a useful one. For my particular use case I wanted to do a synchronous XHR, in a worker thread, returning a binary data response back. It turned out to be surprisingly difficult to get all three requirements working at once with the various packages that I tried.

This PR is similar to the solution in #155, populating response using Buffer.concat, but with some further improvements.

By setting response.setEncoding('binary'), the received data is not lossy encoded to 'utf8' before being stored in the response Buffer. This fixes a problem that exists even in #155 where certain bytes are incorrectly converted in the response buffer, irreversibly becoming 0xFD.

Since the default encoding is now binary, the response data is explicitly encoded to utf8 when storing it in responseText, maintaining the default behaviour of the module before the change.

The same changes are made to the synchronous version of the code. Since part of this process is for data to be temporarily written out to a file, the data buffer is written out base64 encoded here to avoid the same issues with utf8 encoding/decoding when loading the data back in from the file.

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.

1 participant