Skip to content

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